[英]Selenium button click not working using button.click()
我試圖加載一個頁面並按下按鈕,但似乎我做錯了什么。 我以前知道這些事情,但現在新的 selenium 更新讓事情變得更加困難。
這是代碼。
import selenium
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
browser = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\chromedriver\chromedriver.exe")
driver = webdriver.Chrome()
driver.get("https://quizlet.com/217866991/match")
time.sleep(5)
button = browser.find_element(By.CLASS_NAME,"UIButton UIButton--hero")
# Click the button
button.click()
我多次嘗試尋找解決方案,但沒有成功。
這里有幾個問題:
WebDriverWait
expected_conditions
而不是硬編碼延遲。UIButton
UIButton--hero
是多個class 名稱值。 要使用它們,您需要使用 CSS_SELECTOR 或 XPATH,而不是 CLASS_NAME,因為 CLASS_NAME 接收單個值。以下代碼有效:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://quizlet.com/217866991/match/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".UIButton.UIButton--hero"))).click()
結果畫面是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.