【问题标题】:how to put text btween html tag with python webdriver如何使用python webdriver在html标签之间放置文本
【发布时间】:2016-09-25 22:34:35
【问题描述】:

如何使用 python WebDriver 在 html 标签之间放置文本?

<!--html cod-->
<span class="py"></span>  
<!--I want to put text between span tag with python WebDriver like this-->
<span class="py">text</span>

【问题讨论】:

  • @orde:这个问题与在 DOM 元素中插入文本有关;这个问题是关于在 元素之间插入文本。

标签: python html python-2.7 python-3.x selenium-webdriver


【解决方案1】:

您可以在要添加文本之前使用 selenium 获取元素,然后使用 javascript 在该元素的末尾立即插入文本。

例如:

node = driver.find_element_by_xpath("//span[@class='py']")
script = "arguments[0].insertAdjacentHTML('afterEnd', arguments[1])"
driver.execute_script(script, node, "the new text")

这是一个完整的工作示例,假设网站至少有一个 div(在我写这篇文章时它就是这样):

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.example.com")

node = driver.find_element_by_xpath("/html/body/div")
script = "arguments[0].insertAdjacentHTML('afterEnd', arguments[1])"
driver.execute_script(script, node, "the new text")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2014-04-04
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多