这是我的第一个 selenium 项目,我正在尝试提交此表单: https://docs.google.com/forms/d/e/1FAIpQLSdwD1y2eBOoJ-hvk97wDGptfI9oYga8SqtUz7u3nrFbWM7hxw/viewform
我成功点击了多项选择和复选框,但我无法选择下拉菜单。我尝试跟随但没有成功。欢迎大家指点:
import os
import requests
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdwD1y2eBOoJ-hvk97wDGptfI9oYga8SqtUz7u3nrFbWM7hxw/viewform")
multiFirst = driver.find_element_by_xpath("//*[@id=\"i5\"]/div[3]/div")
multiFirst.click()
checkBocSecond = driver.find_element_by_xpath("//*[@id=\"i22\"]/div[2]")
checkBocSecond.click()
dropdownThird = driver.find_element_by_xpath("//*[@id=\"mG61Hd\"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[2]/div[5]")
ActionChains(driver).move_to_element(dropdownThird).click(dropdownThird).perform()
答案 0 :(得分:0)
dropdownThird = driver.find_element_by_xpath(
'(//span[@class="quantumWizMenuPaperselectContent exportContent"])[1]')
dropdownThird.click()
WebDriverWait(driver, 5000).until(EC.presence_of_element_located(
(By.XPATH, '(//*[@jscontroller="liFoG"]//*[contains(text(),"Option 1")])[2]'))).click()
您的定位器在定位器上方使用不正确
你必须导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
答案 1 :(得分:0)
Selenium 提供 Select 方法来选择下拉列表。
从 selenium.webdriver.support.select 导入选择
selection =Select(driver.find_element_by_xpath("yourXPATH"))
3 种选择方式
select_by_index
selection.select_by_index(yourIndex)
它按从 0 开始到下拉列表末尾的索引进行选择。
select_by_visible_text
selection.select_by_visible_text('yourText')
它使用下拉菜单中的文本。
select_by_value
selection.select_by_value('Value')
它使用下拉列表的 value 属性。