Home > Mobile >  my xpath is correct, but selenium doesn't see the element
my xpath is correct, but selenium doesn't see the element

Time:09-15

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='Вернуться к старому дизайну']"}

HTML code

What am I doing wrong? Why doesn't Selenium find him? Because of focusable="false" and aria-hidden="true"? If because of this, how do I identify this element and click on it?

CodePudding user response:

To access the the SVG element,the xpath should be different. 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

CodePudding user response:

I can't tell based on your cropped screenshot, but this element may be in an Iframe. If it's the case, you need to switch your WebDriver's view to this frame.

iframe = driver.find_element(By.WHATERVER, "Whatever")
driver.switch_to.frame(iframe)

Then you can get the element you want.

  • Related