Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selenium fails to locate element even though it's loaded and not in an iframe

I’m trying to locate an element in a form, but for some reason Selenium keeps throwing an error saying it can’t be found. Here is the simple code that I’m using. Can anyone decipher why this isn’t working? It looks like a basic HTML form.

driver = uc.Chrome()
driver.get('https://www.stumblechat.com/register')
username = driver.find_element(By.ID, 'user')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are 2 problems here:

  1. You have to wait for element appearance. The best approach is to use WebDriverWait expected_conditions explicit waits.
  2. That element has different locator. You probably looking for element located by this CSS Selector: input[name='username'].
    If so, your code can be like following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://stumblechat.com/register"

driver.get(url)
username = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading