gpt4 book ai didi

python - 如何使用 Python 使用 Selenium Webdriver 获取此 html 中的某些文本值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:50 25 4
gpt4 key购买 nike

如何使用 selemiun 和 python 从以下 html 代码中获取文本值“This Text 1”和“This Text 2”:

<p>
<b>Studio:</b>
<a href="http://www.somelink.com/some_page/">This Text 1</a>
" | "
<b>Director:</b> This Text 2
</p>

我尝试过多种方法,例如:

driver.find_element_by_xpath("//*[contains(text(), 'Director:')]")

但没有得到结果。

最佳答案

获取文本This Text 1This Text 2使用以下选项。

要获取This Text 1,请使用以下xpath

print(driver.find_element_by_xpath("//p/b[contains(.,'Studio')]/following::a").text)
<小时/>

使用JavaScript Executor获取This Text 2

element=driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath("//p[contains(.,'Director')]"))
print(element.strip())

关于python - 如何使用 Python 使用 Selenium Webdriver 获取此 html 中的某些文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com