【发布时间】:2020-12-22 18:17:36
【问题描述】:
下面的第一个xpath在大多数情况下可以找到我想要的,但在某些情况下xpath略有不同,这使得解决问题变得更加困难。
大多数情况下是第一个 xpath -->
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/ul/div/li[3]/ul/li[4]/div/a').click()
在某些情况下是第二个 xpath-->
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/ul/div/li[5]/ul/li[4]/div/a').click()
所以我正在寻找另一种通过特定文本查找 xpath 的方法。
另一种方式-->
driver.get(url)
html_source = driver.page_source
if "the specific text" in html_source:
<<find xpath where the specific text is>>
<div ext:tree-node-id="7" class="x-tree-node-el x-tree-node-leaf x-unselectable text" unselectable="on"><span class="x-tree-node-indent"><img alt="" src="/js/extjs/resources/images/default/s.gif" class="x-tree-elbow-line"></span><img alt="" src="/js/extjs/resources/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow"><img alt="" src="/js/extjs/resources/images/default/s.gif" class="x-tree-node-icon" unselectable="on"><a hidefocus="on" class="x-tree-node-anchor" href="#" tabindex="1"><span unselectable="on">the specific text</span></a></div>
有可能还是有其他方法?
【问题讨论】:
-
你的xpath和html不同步,分享一下相关的html。而且看起来很笼统,请具体。请阅读minimal reproducible example 并相应地编辑您的帖子,并在以后的所有帖子中遵循相同的内容