Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

python - Handling Drop-down for Google form using selenium

can some one help how to automate drop-downs in Google Forms.As when i tried writing the X-Path's for the drop down instead of having Select and option elements it has div element followed by content element.Also when i try to access the xpath and send the values using sendKeys it's throwing an error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

instead of using sendKeys you should use Select() for drop down as below:

Ex. If you have gender as drop down and you need to select gender you can select as below:

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

Update:

Select won't work if tag is not select for your purpose here's the live demo:

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...