我正在尝试分页搜索结果:Becoming Amazon search。我得到一个'NoSuchElementException'..'Unable to locate element: < insert xpath here >
以下是html:
<div id="pagn" class="pagnHy">
<span class="pagnLink">
<a href="/s/ref=sr_pg_2?rh=...">2</a>
</span>
</div>
以下是我试过的xpath:
driver.find_element_by_xpath('//*[@id="pagn" and @class="pagnLink" and text()="2"]')
driver.find_element_by_xpath('//div[@id="pagn" and @class="pagnLink" and text()="2"]')
driver.find_element_by_xpath("//*[@id='pagn' and @class='pagnLink' and text()[contains(.,'2')]]")
driver.find_element_by_xpath("//span[@class='pagnLink' and text()='2']")
driver.find_element_by_xpath("//div[@class='pagnLink' and text()='2']")
如果我只是使用find_element_by_link_text(...)
,那么有时会选择错误的链接。例如,如果评论数等于我要查找的页码(在本例中为2),则它将选择包含2个评论的产品,而不是页码“2”。
您试图在同一谓词中混合来自不同web元素的属性和文本节点。你应该试着把它们分开如下:
当我查看标记时,我看到了以下内容:
因此,您希望找到一个类为
pagnLink
的span
,该类有一个子元素a
,其文本为2
,或者:有时,最好采取中间步骤,首先获取包含结果的元素。 然后你只需在这个元素中搜索。 这样做可以简化搜索条件。
相关问题 更多 >
编程相关推荐