繁体   English   中英

如何查找没有 id 的元素 selenium python

[英]How to find element without id selenium python

我的目标是自动化 github 上的无聊任务

什么:我想点击“添加文件”而不是“创建新文件” 在此处输入图像描述

我不确定如何获取没有 id 的元素在此处输入图像描述

如果没有 id 或元素有动态 id,则使用 XPATH。 使用 class 并不理想,因为 class 可能会被许多其他元素重用。

在这种情况下,使用这个 XPATH: //summary[.//span[contains(text(),"Add file")]]

最佳实践:使用流利的等待

WebDriverWait(driver, 15).until(
        EC.visibility_of_element_located(
                (By.XPATH, '//summary[.//span[contains(text(),"Add file")]]')
        ))

阅读有关显式/流畅等待的文档: https://selenium-python.readthedocs.io/waits.html#explicit-waits

你可以试试driver.find_element_by_xpath("[xpath goes here]")

如果你想了解更多,这里是定位元素的官方文档。

from selenium import webdirver

#define your executable path 
browser = webdriver.Chrome(executable path="  ") 
browser.get("**")
browser.find_element_by_xpath("put your copied xpath here")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM