首页 > 解决方案 > Python Selenium 如何检索当前的 xpath

问题描述

目前致力于网络任务自动化。

当我访问我想要的网页时,我让 Selenium 使用他们的 Xpath 查找元素

receivedTime = browser.find_elements_by_xpath('//*[starts-with(@id, "anchor") and contains(@id, "_0")]')
for time in receivedTime:

所以它找到了所有这些元素。在 for 循环期间,我将如何检索当前正在使用的 xpath?这样我就可以使用基于当前 xpath 的 If 语句。

例如,如果当前 xpath == "anchor0_0":

谢谢你。

标签: python-3.xseleniumfor-loopselenium-webdriverxpath

解决方案


您可以检查@id每个元素,如下所示:

receivedTime = browser.find_elements_by_xpath('//*[starts-with(@id, "anchor") and contains(@id, "_0")]')
for time in receivedTime:
    if time.get_attribute('id') == 'anchor0_0':
        # Do something

推荐阅读