Steve Bermeo
Steve Bermeo

Reputation: 37

Filling out fields using selenium (poor html here)

I am trying to accomplish the following things:

Here's the code I have so far:

PATH = "D:\Program Files (x86)\chromedriver.exe"
email = "yada@yada"

option = webdriver.ChromeOptions()
option.add_argument("-incognito")
option.add_experimental_option("excludeSwitches", ['enable-automation']);
browser = webdriver.Chrome(executable_path=PATH, options=option)

browser.get("https://WEBSITE.com/")


email_field = browser.find_element_by_xpath('//*[@id="email"]')
email_field.send_keys(email)

time.sleep(5)
browser.close()

The error this prompts when runningis:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="email"]"}
  (Session info: chrome=106.0.5249.119)


Process finished with exit code 1

I don't know much about html, but I see that the email field is inside of an iframe

<div class="form-row">
      <input type="text" id="email" name="email" placeholder="Enter your email:" data-validate="required,email">

      <div class="required-message">Oops! You forgot your Text here</div>
      <div class="invalid-message">Oops! Invalid email address</div>
    

Where is my approach failing? All I want is to enter the email field, press enter, and close the browser after 5 seconds.

Edit:

Trying the suggestion below, prompts this error:

Traceback (most recent call last):
  File "C:/Users/ME/AppData/Roaming/JetBrains/PyCharmCE2021.1/scratches/ChromeBot.py", line 18, in <module>
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "email")))
  File "C:\Users\ME\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Could this be my path? I stored the chrome driver on the D drive

Upvotes: 0

Views: 56

Answers (1)

jrynes
jrynes

Reputation: 185

For the first point, about the error message - Selenium is saying that it can't find the target element you're looking for - Sometimes this is due to your script executing before the page has fully loaded.

To your second point about the iframe - You'll want to switch the driver to the iframe first, then use the sendkeys function on that iframe

You may be able to try something like below:

    browser.get("https://WEBSITE.com/")
    
    # Adding the line below to wait until the target element is present, or 10 seconds have passed
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.id, "email")))
    
    # Switch to iframe
    driver.switch_to.frame(browser.find_element_by_id('your_iframe_id_here'))
    
    # Then get the target element, and send keys
    email_field = browser.find_element_by_xpath('//*[@id="email"]')
    email_field.send_keys(email)

The thread below may help with waiting until the page has fully loaded to execute your script:

Selenium - wait until element is present, visible and interactable

The thread below may help with the iframe a bit more:

Python Selenium can't find any element on a webpage

Upvotes: 1

Related Questions