Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python webdriver在html标记之间放置文本_Python_Html_Python 2.7_Python 3.x_Selenium Webdriver - Fatal编程技术网

如何使用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获取元素,然后使

如何使用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获取元素,然后使用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")