簡體   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