Home > Mobile >  Selenium Unable to locate field by ID
Selenium Unable to locate field by ID

Time:10-21

I am trying to do automatic reservation on a website, I found a way to select the day and the hour. But when I try to fill the form, Selenium can't find the element by ID.

The website (it's in french) : https://www.billetweb.fr/comptoir

My code :

import selenium
from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get("https://www.billetweb.fr/comptoir")
time.sleep(2)
driver.switch_to_frame(driver.find_element_by_id("eventcomptoir"))
day = driver.find_elements_by_class_name("ui-state-default")
#I will add a way to choose the good day later
day[24].click()
time.sleep(1)
hour = driver.find_elements_by_class_name("sesssion_href_table")
#The button is sometimes at the first position of the list (don't know how)
try:
    hour[1].click()
except selenium.common.exceptions.ElementNotInteractableException:
    hour[0].click()
time.sleep(1)
first_confirm = driver.find_elements_by_class_name("nextButton")
first_confirm[4].click()

#error here
driver.find_element_by_id("field_3_0_").send_keys("my first name")
driver.find_element_by_id("field_2_0_").send_keys("my last name")
driver.find_element_by_id("field_1_0_").send_keys("my email")
driver.find_element_by_id("field_email2__").send_keys("my email")

The error (line 24) :

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="field_3_0_"]"}

CodePudding user response:

You need to apply some Waits to find the elements and interact with them. Try like below and confirm:

# Imports Required for Explicit wait:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://www.billetweb.fr/comptoir")
wait = WebDriverWait(driver,30)
... # Code to select options.

wait.until(EC.element_to_be_clickable((By.ID,"field_3_0_"))).send_keys("Sample Text")
wait.until(EC.element_to_be_clickable((By.ID,"field_2_0_"))).send_keys("Sample Text")
wait.until(EC.element_to_be_clickable((By.ID,"field_1_0_"))).send_keys("Sample Text")
wait.until(EC.element_to_be_clickable((By.ID,"field_email2__"))).send_keys("Sample Text")
  • Related