繁体   English   中英

使用硒处理 Google 表单的下拉列表

[英]Handling Drop-down for Google form using selenium

有人可以帮助如何在 Google Forms 中自动化下拉列表。当我尝试为下拉列表编写 X 路径而不是选择和选项元素时,它具有 div 元素,后跟内容元素。此外,当我尝试访问xpath 并使用 sendKeys 发送值会引发错误。

而不是使用sendKeys你应该使用Select()下拉如下:

例如。 如果您有性别作为下拉菜单,并且您需要选择性别,您可以选择如下:

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.webdriver import WebDriver

driver = WebDriver()  # setup web driver
driver.get(<url>)  # retrive url
gender_select = Select(driver.find_element_by_name('gender'))  # get element by name or id or xpath
gender_select.select_by_visible_text('Male')  # here 'Male' is the text displayed on page so you can select item from dropdown menu by text visible in drop down menu

更新:

如果未根据您的目的选择标签,则Select将不起作用,这里是现场演示:

def foo(url="https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform", opt="Option 2", delay=20):
    from selenium.webdriver.chrome.webdriver import WebDriver
    import time

    driver = WebDriver()
    driver.get(url)
    driver.find_element_by_class_name("quantumWizMenuPaperselectOptionList").click()
    options=driver.find_element_by_class_name("exportSelectPopup")
    time.sleep(3)
    print(options)
    contents = options.find_elements_by_tag_name('content')
    [i.click() for i in contents if i.text == opt]

foo()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM