Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...