如何使用python webdri在html标记之间放置文本bt

2024-04-26 07:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用pythonwebdriver在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>

Tags: totextpy标记文本puthtmltag
1条回答
网友
1楼 · 发布于 2024-04-26 07:07:25

您可以在添加文本之前使用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(在我撰写本文时,它是这样做的):

^{pr2}$

相关问题 更多 >