我希望創建一個自動填寫 Google 表單調查的機器人,其中填充了單選按鈕。當我嘗試填寫表格時,第一個性別單選按鈕已成功單擊。但是,第二個年齡段的按鈕不是嗎?這是一個錯誤嗎?
import time
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/Users/ryan/Downloads/chromedriver.exe')
browser.get('https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBB/viewform?fbzx=-1760864547538697754')
RadioGender= browser.find_element_by_xpath('//*[@id="i5"]/div[3]/div')
RadioGender.click()
RadioAge= browser.find_element_by_xpath('//*[@id="i18"]/div[3]/div/div')
RadioAge.click()
uj5u.com熱心網友回復:
處理動態元素使用WebDriverWait()并等待元素可點擊并遵循locator策略。
browser.get("https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBBt1MqEgoQOz9oHWWmTY1Q/viewform?fbzx=-1760864547538697754")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Next"]'))).click()
RadioGender= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@data-value="Male"]')))
RadioGender.click()
RadioAge= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@data-value="21-23"]')))
RadioAge.click()
您需要匯入以下庫。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
注意:如果您想選擇其他年齡范圍,您需要將這些值作為資料值傳遞。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477083.html
標籤:Python 硒 硒网络驱动程序 路径 网络驱动程序等待
上一篇:進入新頁面前需要登錄
