0

I am learning Python, specifically browser automation now and the code:

from selenium import webdriver

browser = webdriver.Chrome()
browser.get("https://github.com")
browser.maximize_window()

signin_link = browser.find_element_by_link_text("Sign in")
signin_link.click()

username_box=browser.find_element_by_id("login_field")


browser.quit()

raises a NoSuchElementException, when according to the console:

<input type="text" name="login" id="login_field" class="form-control input-block js-login-field" autocapitalize="off" autocorrect="off" autocomplete="username" autofocus="autofocus">

the element exists.

To solve a similar problem where looking for the "Sign in" button would raise the same exception, please be aware that I have switched from selenium v4.3.0 to selenium v3.141.0.

The code you are seeing is nearly identical to the code that I was shown in my Python course, except for the 'browser.maximize_window()' method, only there because the "Sign in" button would not exist otherwise. The video where the code was shown shows everything working fine.

However, the same problem has popped up again.

I have tried the following:

  • let Python wait for a certain amount of time between opening the login page and finding the element, using both 'implictly wait' and 'time.sleep' as well as 'WebDriverWait'
  • make Python wait and then work from the outermost element with the id being 'login_field' to the element where the id is 'login_field'. (I do not know how to tell an iframe within the HTML source code, and this solution did not work)
  • using 'find_element_by_class_name' instead of 'find_element_by_ID'.

None of these solutions worked!

Can you please tell me why the exception is raised even if the element exists, and general solutions that will fix the problem, and if you suspect if the element is within an iframe, tell me how to tell an iframe within the browser console?

Daredevil
  • 1
  • 1

2 Answers2

0

I had solved the problem with the help of this URL NoSuchElementException and SyntaxError during web-scraping, and by updating back to selenium v4.3.0 (no longer raises an exception, the mistake was that I did not maximize the window first).

To solve a similar problem, first use version v4.3.0 (Selenium 3.141.0 closes the browser when I try to use WebDriverWait and I have not seen bugs yet with v4.3.0), then maximize the window to ensure that the element can be seen, and then use WebDriverWait to wait until the element is visible (the element should be visible to Selenium in a second). If that does not work, search for the exception on Google.

Daredevil
  • 1
  • 1
0

It worked for me after adding WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "login_field"))) in your code. Most probably element id="login_field" takes some time to load and username_box=browser.find_element_by_id("login_field") line executes before the loading is finished which causes the element not found error. For me it works for Selenium 3.141.0. Also for me it worked even if I remove the browser.maximize_window().

Here's the full code:

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

browser = webdriver.Chrome()
browser.get("https://github.com")
browser.maximize_window()

signin_link = browser.find_element_by_link_text("Sign in")
signin_link.click()

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "login_field")))
username_box=browser.find_element_by_id("login_field")

browser.quit()
zafi005
  • 500
  • 1
  • 5
  • 12