简体   繁体   English

我的 xpath 是正确的,但是 selenium 没有看到元素

[英]my xpath is correct, but selenium doesn't see the element

My code:我的代码:

driver.find_element_by_xpath("//svg[@class='SvgIcon_root__keb_Y'][@aria-label='Вернуться к старому дизайну']").click()

My error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//svg[@class='SvgIcon_root__keb_Y'][@aria-label='Вернуться к старому дизайну']"}我的错误: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//svg[@class='SvgIcon_root__keb_Y'][@aria-label='Вернуться к старому дизайну']"}

HTML code HTML代码

What am I doing wrong?我究竟做错了什么? Why doesn't Selenium find him?为什么Selenium找不到他? Because of focusable="false" and aria-hidden="true"?因为 focusable="false" 和 aria-hidden="true"? If because of this, how do I identify this element and click on it?如果是因为这个,我如何识别这个元素并点击它?

To access the the SVG element,the xpath should be different.要访问SVG元素, xpath应该不同。 It should be like.应该是这样的。

//*[local-name()='svg' and @aria-label='Вернуться к старому дизайну']

Or或者

//*[local-name()='svg' and @class='SvgIcon_root__keb_Y']

Or或者

//*[name()='svg' and @aria-label='Вернуться к старому дизайну']

Ideally your code should be like理想情况下,您的代码应该像

driver.find_element_by_xpath("//*[local-name()='svg' and @aria-label='Вернуться к старому дизайну']").click()

You can find following reference, how to interact with svg element xpathforsvgelement您可以找到以下参考,如何与svg元素xpathforsvgelement交互

I can't tell based on your cropped screenshot, but this element may be in an Iframe.根据您裁剪的屏幕截图,我无法判断,但此元素可能位于 Iframe 中。 If it's the case, you need to switch your WebDriver's view to this frame.如果是这种情况,您需要将 WebDriver 的视图切换到此框架。

iframe = driver.find_element_by_xpath("Put the Iframe Xpath here")
driver.switch_to.frame(iframe)

Then you can get the element you want.然后你可以得到你想要的元素。

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

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