简体   繁体   English

无法定位元素 XPath (Selenium)

[英]Unable to locate element XPath (Selenium)

I am trying to use this XPath in Selenium with Python but it's generating an error: 'Message: no such element: Unable to locate element':我试图在带有 Python 的 Selenium 中使用这个 XPath,但它产生了一个错误:“消息:没有这样的元素:无法定位元素”:

driver.find_element(By.XPATH, '//div[contains (@class, "files-list-grid-view")]/div[1][contains (@class, "folder")]').click()

This code works in Dev Tools with $x().此代码在开发工具中使用 $x() 工作。

在此处输入图片说明

I need a marked element.我需要一个标记的元素。

If the dataref node value is unique and it is not changing dynamically then you can use the below xpath :如果dataref节点值是唯一的并且没有动态更改,则可以使用以下 xpath :

element = driver.find_element_by_xpath("//div[@dataref='folder-0']")

If you are still getting an error then try to give some delay before locating it like below :如果您仍然收到错误,请尝试在找到它之前稍作延迟,如下所示:

from time import sleep

sleep(3)
element = driver.find_element_by_xpath("//div[@dataref='folder-0']")
# And perform some action here

Still it doesn't work then check for frame or iframe using //iframe or //frame locators and if there are any matches and the locator that you are trying to locate is in some frame then switch to it using the below line and try to run your code again :仍然不起作用,然后使用//iframe//frame定位器检查frameiframe ,如果有任何匹配项并且您尝试定位的定位器在某个框架中,则使用以下行切换到它并尝试再次运行您的代码:

from time import sleep

# Switch to corresponding frame
driver.switch_to_frame("frame locator")

# Wait for sometime
sleep(3)
# Try to find an element
element = driver.find_element_by_xpath("//div[@dataref='folder-0']")
# And perform some action here

I hope it works...我希望它有效...

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

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