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?