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

Question:

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?

Asked By: Dmitrii Gavrilov

||

Answers:

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.

Answered By: Wutong

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

Answered By: KunduK
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.