Can't find XPATH element

50 Views Asked by At

I am dealing with this HTML:

<div class="IaVba F1" data-test-target="review-rating">
   <svg class="UctUV d H0" viewBox="0 0 128 24" width="88" height="16" aria-label="4,0 de 5 burbujas">

I want to get the attribute "aria-label" from the SVG element. Finding the DIV with "review-rating" works fine in XPATH:

element = review.find_element(By.XPATH, './/div[contains(@data-test-target,"review-rating")]')

But if I go to the following element that is the SVG it is not working an I received the error:

Message: no such element: Unable to locate element: {"method":"xpath","selector":".//div[contains(@data-test-target,"review-rating")]/svg"}

element = review.find_element(By.XPATH, './/div[contains(@data-test-target,"review-rating")]/svg')

If I could position to the svg then I could retrieve the "aria-label" attribute. Any idea of what am I doing wrong?

1

There are 1 best solutions below

1
Siebe Jongebloed On

svg elements are in a different namespace than html. Therefore you cannot use xpath as if they were html-elements. See this tutorial for more clarification

So in your case you would need this xpath:

.//div[contains(@data-test-target,"review-rating")]/*[local-name()="svg"]