0

I have a nested element possibly within <svg> that I can't seems to access

I tried using

driver.find_element(By.CSS_SELECTOR, 'button.login-fake-btn')

and

driver.find_element(By.CSS_SELECTOR, 'login-fake-btn')

and a few others.

HTML structure of nested svg:

<svg class="1">
<div id="2">
<div>
<div class="3">
<div class="4">
<li>
<button class="5" type="button" id="login-fake-btn">
...closing tags

Snapshot of HTML:

HTML

I have no success with xpath either.

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.login-fake-btn"}

How do I get to a nested svg using a css selector (or xpath, but I understand css to be better)?

3
  • Shouldn't that be button#login-fake-btn or #login-fake-btn? Commented Jul 20, 2020 at 18:21
  • 1
    . is for class, # is for id Commented Jul 20, 2020 at 18:21
  • Can you provide complete HTML of svg element which contains the button? Commented Jul 20, 2020 at 19:47

1 Answer 1

1

It's a <button> element and it's out of the <svg> tag and possibly moving forward you'd invoke click() on it. Hence to locate the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='login-btn btn-shadow' and @id='login-fake-btn'][@data-testid='login-fake-btn']")))
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

Sign up to request clarification or add additional context in comments.

4 Comments

I tried that and a couple of variations i.imgur.com/l9AyPjZ.png with a timeoutexception error
@brad Checkout the updated answer and let me know the status.
I get the following error selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
@brad Sounds great, so we identified the element correct. You never told us you would click :) that was my assumption. Okay, now without the text based HTML and the complete error trace log it's hard to guess anything more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.