Selenium Python:包含链接文本的类
我正在使用Python和Selenium删除某个网页的内容。目前,我有以下问题:有多个同名的div类,但每个div类都有不同的内容。我只需要一个特定div类的信息。在下面的示例中,我需要第一个“show_result”类中的信息,因为链接文本中有“重要元素”:Selenium Python:包含链接文本的类,python,css,selenium,Python,Css,Selenium,我正在使用Python和Selenium删除某个网页的内容。目前,我有以下问题:有多个同名的div类,但每个div类都有不同的内容。我只需要一个特定div类的信息。在下面的示例中,我需要第一个“show_result”类中的信息,因为链接文本中有“重要元素”: <div class="show_result"> <a href="?submitaction=showMoreid=77" title="Go-here"> <span class="ne
<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>
Ups,我通过xpath自己找到了解决方案:
driver.find_element_by_xpath("//div[contains(@class, 'show_result') and contains(., 'Important-Element')]")
driver.find_element_by_xpath("//div[contains(@class, 'show_result') and contains(., 'Important-Element')]")
我知道您已经找到了答案,但我认为这是错误的,因为您还将选择其他节点,因为重要元素仍然位于非重要元素中 也许它适用于你的具体案例,因为这不是你真正想要的文本。但这里还有一些答案:
//div[@class='show_result'并以(,'Important-Element')开头]
//div[span[text()=“重要元素”]
//div[contains(span/text(),'Important-Element')和not(contains(span/text(),'Non'))]
有更多的方法来写这个…你可以把它作为答案添加并接受:)谢谢,你说得对。我的解决方案在我的案例中起作用,因为元素的名称不是“重要元素”和“不重要元素”等。它们有相互排斥的名称,如“斑马”、“大猩猩”等。因此,我的xpath包含了有效的名称。