如何使用python webdriver在html标记之间放置文本
如何使用python WebDriver在html标记之间放置文本如何使用python webdriver在html标记之间放置文本,python,html,python-2.7,python-3.x,selenium-webdriver,Python,Html,Python 2.7,Python 3.x,Selenium Webdriver,如何使用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> 文本 您可以在添加文本之前使用selenium获取元素,然后使
<!--html cod-->
<span class="py"></span>
<!--I want to put text between span tag with python WebDriver like this-->
<span class="py">text</span>
文本
您可以在添加文本之前使用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(我写这篇文章时它就是这样做的):
@orde:这个问题与在DOM元素中插入文本有关;这个问题是关于在元素之间插入文本的。
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")