Selenium无法通过ID定位字段



我想在一个网站上做自动预订,我找到了一种选择日期和时间的方法。但是当我尝试填写表单时,Selenium无法通过ID找到元素。

网站(法语):https://www.billetweb.fr/comptoir

我的代码:
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")
错误(第24行):
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="field_3_0_"]"}

您需要应用一些等待找到元素并与之互动。尝试如下并确认:

# 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")

相关内容

  • 没有找到相关文章

最新更新