我正在嘗試在網站上進行自動預訂,我找到了一種選擇日期和時間的方法。但是當我嘗試填寫表單時,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_"]"}
uj5u.com熱心網友回復:
您需要應用一些等待來查找元素并與它們互動。嘗試如下并確認:
# 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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329951.html
上一篇:使用seleniumWebDriver為站點制作自動機。但是由于某種原因,xpath帶有下劃線。有誰知道這是什么原因?
