0

HTML code is in enclosed image html code snap

Locating span element "Users" failed with below error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'Users')]"}

Tried locating "Users" tab with below code

users = driver.find_element(By.XPATH, "//span[contains(text(),'Users')]")
3
  • Can you please, as described in selenium-webdriver tag description add tag for language you're using? It's Python, right? It would also help if you could post relevant HTML snippet as text rather than image, as per How to Ask: "DO NOT post images of code, data, error messages, etc.—copy or type the text into the question." Commented Mar 28, 2023 at 9:22
  • Share the URL of the application if its not a sensitive info. Commented Mar 28, 2023 at 9:41
  • @Shawn This is our internal web application so can't be reachable outside our VPN. Commented Mar 28, 2023 at 10:20

2 Answers 2

1

There could be multiple reasons for this Exception. Without accessing the actual URL, its hard to tell the root cause. Below are few possible options you can try:

1. First check if you can locate the element by inspecting the DOM.

Press F12 key, and then do Ctrl + F. Paste the XPATH expression //span[contains(text(),'Users')]. If it locates one element then you are good, if it locates more than one element or locates zero elements you need to modify your XPATH expression

2. Check for the presence of iframe, if the desired element is within an iframe, then you need to switch into the iframe first. Refer below link:

https://stackoverflow.com/a/75812495/7598774

3. If there is no iframe, then try to perform action by inducing waits.

users = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Users')]" )))

Imports required:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Shawn for the inputs! Second point in your post help to trace this problem further & found multiple "iframe". Issue resolved after switching into corresponding "iframe" before locating "Users" element.
0

Your xpath is correct. Use Selenium waits and try to find. If the element is not interactable you will get NoSuchElementException. To avoid this, selenium provides waits. Instead you can try with Thread.sleep() to wait for sometime.

1 Comment

Adding delay is not working with same error of NoSuchElementException & I believe if the element is not interactable then probably exception would be "selenium.common.exceptions.ElementNotInteractableException"

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.