在 python 中使用不带 select 标签的 selenium 专门选择 google 表单下拉列表

Selecting specifically google form dropdown using selenium without select tag in python

我一直在尝试通过 selenium 和 python 从 google 表单中找到 select 下拉选项的方法,但迄今为止没有成功。

我尝试了几种方法,包括 Select class(由于下拉表单未使用 select 标签,该方法不起作用)。以及 XPATH。但目前只能点击下拉列表,但无法 select 所述下拉列表中的选项。任何帮助将不胜感激!

我需要能够根据各种选项中的 text/value 指定要选择的下拉选项。

代码:

browser.find_element_by_xpath("//div[@class='quantumWizMenuPaperselectOptionList']").click() 
browser.find_element_by_xpath("//div[@class='freebirdThemedSelectOptionDarkerDisabled']/div[@class='quantumWizMenuPaperselectOption'][@data-value='1.05pm - 3.55pm']").click()

我得到的错误是没有找到这样的元素,即使这是我通过相应地检查下拉菜单找到的 XPATH。

我在这里创建了一个示例表格以供参考:https://forms.gle/prBMqgVVFNv5KWQQA

这些没有帮助,因为它使用 Select class,请不要将此问题标记为重复问题

How to select/get drop down option in Selenium 2

How to select a drop-down menu value with Selenium using Python?

使用 webdriverwait 似乎也没有达到 post:

中详述的技巧

问题是 selection 框不会弹出,即使你的 xpath 是正确的,直到你悬停在那个元素上,悬停在那个元素上让它可以点击。您可以使用以下代码将鼠标悬停在 selection 框元素上,然后尝试单击 select 元素

selectBox = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@role='listbox']")))
action = ActionChains(browser);
action.move_to_element(selectBox).perform()

完整代码在这里:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
url = 'https://docs.google.com/forms/d/e/1FAIpQLScosZjmDrvgUvh77tXsaAb24hKVgaBnjJfJz2BX1PvoqIO1Ow/viewform'
text = '1.05pm-3.55pm'
browser.maximize_window()
browser.get(url)
selectBox = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@role='listbox']")))
action = ActionChains(browser);
action.move_to_element(selectBox).perform()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@role='listbox']")))
selectBox.click()

selectionXpath = "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup appsMaterialWizMenuPaperselectPopup']//span[@class='quantumWizMenuPaperselectContent exportContent' and text()='"+text+"']"
selection = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, selectionXpath)))
selection.click()

请注意,由于我尚未提交表格,因此我尚未验证 selection 的这种方式是否保留了表格中的信息。你可以测试它验证它。