我正在使用Python Selenium,并且使用以下HTML结构:
<div>
<h2>Welcome</h2>
<div>
<p>some text <strong>important</strong></p>
<a>link</a>
</div>
</div>
我想要从父div的每个后代(h2,div,p,strong,a)中获取文本,例如,对于<p>标签I want some text。
我一直在使用.text属性,并改为使用some text important。我想使用类似于BeautifulSoup属性.string的东西。
编辑:我需要的代码,以工作于任何父元素包含的子代与更多嵌套的子代-不仅仅是这个特定的HTML结构。
谢谢你的帮助。
发布于 2019-08-09 21:18:45
使用Java Script executor返回textContent。
print(driver.execute_script('return arguments[0].firstChild.textContent;', driver.find_element_by_xpath("//h2[contains(.,'Welcome')]/following::div/p")))https://stackoverflow.com/questions/57430608
复制相似问题