viv
viv

Reputation: 3

Unable to locate span element "Users" with selenium-Python

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')]")

Upvotes: 0

Views: 780

Answers (2)

Shawn
Shawn

Reputation: 8563

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

Upvotes: 1

Keerthana J
Keerthana J

Reputation: 1

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.

Upvotes: 0

Related Questions