【发布时间】:2017-02-05 06:41:00
【问题描述】:
我正在使用 Python 和 Selenium 来废弃某个网页的内容。目前,我有以下问题:有多个同名的div-class,但每个div-class的内容不同。我只需要一个特定 div 类的信息。在以下示例中,我需要第一个“show_result”类中的信息,因为链接文本中有“Important-Element”:
<div class="show_result">
<a href="?submitaction=showMoreid=77" title="Go-here">
<span class="new">Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=78" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=79" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
通过以下代码,我可以获得“重要元素”及其链接:
driver.find_element_by_partial_link_text('Important-Element')。但是,我还需要同一个 div 类“show-result”中的其他信息。如何在链接文本中引用包含重要元素的整个 div 类? driver.find_elements_by_class_name('show_result') 不起作用,因为我不知道重要元素位于哪个 div 类中。
谢谢, 芬兰人
编辑/更新:Ups,我使用xpath自己找到了解决方案:
driver.find_element_by_xpath("//div[contains(@class, 'show_result') and contains(., 'Important-Element')]")
【问题讨论】:
-
您可以将其添加为答案并接受它:)