0

I try to locate elements on a website which are in a form to then send keys to login but I get an error, see below:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}

Line of code is:

driver.find_element(By.ID, "username").send_keys("test123")

For HTML code see:

HTML code

I use latest version of Python/Selenium and PyCharm.

I hope someone can help.

Coding_89
  • 1
  • 2

2 Answers2

0

I don't see anything wrong in that line of code. Try one of the below

  • Try applying implicit wait as below:
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(20)
  • Check if there is a frame/ iframe covering this element, if yes, try below code before locating the element
iframe = driver.find_element_by_xpath("<XPath expression of the frame>")
driver.switch_to.frame(iframe)

To switch back to the main DOM:

driver.switch_to.default_content()

  • If the frame doesn't exist, then try inducing explicit wait as below:
element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "username"))

Below imports you will require:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Shawn
  • 4,064
  • 2
  • 11
  • 23
0

Given the HTML:

html

the element is a <input> element which would accept text input using send_keys() method.


Solution

Ideally to send a character sequence to an <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username[name='username']"))).send_keys("Coding_89")
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='input' and @id='username'][@name='username']"))).send_keys("Coding_89")
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for the quick answer unfortunately I get the following error on Using XPATH: Traceback (most recent call last): File "C:\Users\123\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: – Coding_89 Feb 08 '23 at 21:54
  • @Coding_89 Check if the desired element is within an [**iframe**](https://stackoverflow.com/a/53276478/7429447) or within a [**shadowRoot**](https://stackoverflow.com/a/73242476/7429447) – undetected Selenium Feb 08 '23 at 21:56
  • I looked a bit deeper into the HTML code and i think its because it is a form not an iframe, i changed the picture in the original post to provide more of the HTML code – Coding_89 Feb 10 '23 at 19:10