在与搜索框进行交互之前,您可能需要首先处理同意弹窗:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Consent"]'))).click()
这段代码需要用到以下导入语句:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
示例
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("https://www.copart.com/")
# 等待并点击同意按钮
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Consent"]'))).click()
search_box = driver.find_element(By.ID, 'mobile-input-search')
search_box.send_keys("72486533")
search_box.send_keys(Keys.RETURN)