简体   繁体   English

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

[英]How to find element without id selenium python

My goal: is to automate a boring task on github我的目标是自动化 github 上的无聊任务

What: I want to click on "add file" and than "create new file"什么:我想点击“添加文件”而不是“创建新文件” 在此处输入图像描述

I am not sure how to get the element without an id我不确定如何获取没有 id 的元素在此处输入图像描述

If no id or the element has dynamic id, use XPATH.如果没有 id 或元素有动态 id,则使用 XPATH。 Using class is not ideal, since the class will possibly reused by many other elements.使用 class 并不理想,因为 class 可能会被许多其他元素重用。

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

Best practice: use fluent wait最佳实践:使用流利的等待

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

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

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

If you want to read more, here are the official docs for locating elements.如果你想了解更多,这里是定位元素的官方文档。

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