user2023630
user2023630

Reputation: 59

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

Upvotes: 1

Views: 146

Answers (1)

Prophet
Prophet

Reputation: 33361

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

Upvotes: 1

Related Questions