Python Selenium如何在google表单中选择下拉选项

2024-09-25 00:33:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法选择此下拉列表的不同选项 字段中的大小

https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform

有人有好办法吗? 我可以用鼠标单击元素

driver.find_element_by_xpath('/html/body/div/div[2]/form/div[2]/div/div[2]/div[14]/div/div/div[2]/div/div[1]/div[1]/div[1]').click()     

但我无法选择不同的选项


Tags: httpsdivcom元素docs列表选项google
2条回答

您需要单击列表以打开它,然后查找并单击包含正确内容的范围。由于有一点绘图延迟,您可能希望它使用webdriverwait来确保元素在交互之前准备就绪

这对我很有用:

driver = webdriver.Chrome()

driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform")


#open the size menu
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='SIZE']/following::div[@class='quantumWizMenuPaperselectOptionList'][1]"))).click()

#select the size by it's full text
sizeToSelect = "US 11"
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'exportSelectPopup')]/div/span[text()='"+sizeToSelect+"']"))).click()

如果尚未获得,则需要以下导入:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

突出显示字号,单击鼠标右键,然后单击“检查”。现在再做一次,它将引导您找到与此相关的HTML。从HTML的根开始往下看,直到找到所需大小的部分(当您将鼠标悬停在代码上时,它将在网站中突出显示相关部分)。希望这将帮助您充分使代码的功能

相关问题 更多 >