-1

I want to select a certain option by automating using python and selenium. I am able to select the text fields by name but I am unsure how to select the dropdowns in the form.

https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform

I have tried using send_keys with getting the element by class but it doesn't seem to work.

driver.find_element_by_class_name("quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption").send_keys("my choice")

How can I select an option of my choice from the dropdown in the above form ?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Zain
  • 35
  • 5

2 Answers2

0

It looks like there are multiple elements that are returned for this class.

.find_element_by_class_name

Above returns the first one it finds which happens to not work. The other strategy is to "try-except-click" all of them. See below.

from selenium import webdriver
import time


driver = webdriver.Firefox(executable_path=r'C:\\Path\\To\\Your\\geckodriver.exe')

driver.get("https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform")

time.sleep(2)

dropdown = driver.find_element_by_xpath("//div[@role='option']")
dropdown.click()
time.sleep(1)

option_one = driver.find_elements_by_xpath("//div//span[contains(., 'Option 1')]")
for i in option_one:
    try:
        i.click()
    except Exception as e:
        print(e)
Jortega
  • 2,423
  • 14
  • 16
0

To select the option with text as Option 2 you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.quantumWizMenuPaperselectOptionList"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.exportSelectPopup.quantumWizMenuPaperselectPopup div.quantumWizMenuPaperselectOption.freebirdThemedSelectOptionDarkerDisabled.exportOption[data-value='Option 2']"))).click()
    
  • Using XPATH:

    driver.get('https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='quantumWizMenuPaperselectOptionList']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup']//div[@class='quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption' and @data-value='Option 2']//span[text()='Option 2']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217