Selenium 无法通过 ID 定位字段

问题描述 投票:0回答:1

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

网站(法语):

redacted

我的代码:

import selenium
from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get("redacted")
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_"]"}
python selenium
1个回答
0
投票

您需要应用一些Waits来查找元素并与它们交互。尝试如下并确认:

# 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")
© www.soinside.com 2019 - 2024. All rights reserved.