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.
Asked
Active
Viewed 3,286 times
1 Answers
2
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()

Gahan
- 4,075
- 4
- 24
- 44
-
Thanks a ton! Worked like a charm :) – Nish Apr 19 '18 at 05:18
-
you can [upvote, accept answer](https://stackoverflow.com/help/someone-answers) if it is helpful – Gahan Apr 19 '18 at 05:18