如何通过复杂的xpath查找元素?

2024-05-11 14:49:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到一个按钮,但不知道如何,因为html代码很复杂。你能帮我吗

HTML

<div style="margin-top: 20px;">
  <span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(99, 99, 102);">Bugsnag akzeptieren
  </span>
  <span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(0, 122, 255);">Alle akzeptieren
  </span>
</div>

因此,我想找到第二个元素,并尝试以下方法:

self.browser.find_elements_by_xpath("//span[@style='the whole text that equals style in the html']")

但它似乎不起作用


Tags: margindivsizestylehtmldisplayinlinergb
1条回答
网友
1楼 · 发布于 2024-05-11 14:49:41

根据您想对其执行的操作,您可以执行//span[2]来选择第二个跨度:

选择元素的示例

self.browser.find_elements_by_xpath("//span[2]")enter image description here

如果需要整个元素


选择文本的示例

self.browser.find_elements_by_xpath("//span[2]/text()")

如果需要第二个跨距的内部html文本

我在这个website上测试了xpath表达式,以确保它正常工作

我复制了您提供的html,并输入了xpath表达式//span[2]/text()来测试它。 enter image description here

有关xpathhere的详细信息

相关问题 更多 >