-1

I am trying to learn selenium but ran into the issue of clicking on dropdown options. Got so frustrated I created an account for this issue.

URL https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform

I added the time function part thinking if the option load I can click on it. Sadly it was a fail.

from selenium import webdriver

browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform?usp=sf_link")
start =browser.find_element_by_xpath('//*[@id="mG61Hd"]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[1]/span')

start.click()
import time 
time.sleep(2)
startt =browser.find_element_by_xpath('//*[@id="mG61Hd"]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[6]/span')
time.sleep(2)
startt.click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

Here is the logic that you can use to select the option.

# this will open the list box
driver.execute_script("arguments[0].click()",driver.find_element_by_xpath("(//div[@role='option'])[1]"))

# this will select the option
driver.find_element_by_xpath("(//div[@role='listitem']//span[.='Option 4'])[2]").click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

To click() on the element with text as Next you need 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/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.quantumWizMenuPaperselectOption.freebirdThemedSelectOptionDarkerDisabled.exportOption.isSelected.isPlaceholder span.quantumWizMenuPaperselectContent.exportContent"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.exportSelectPopup.quantumWizMenuPaperselectPopup div[data-value='Option 4'] span.quantumWizMenuPaperselectContent.exportContent"))).click()
    
  • Using XPATH:

    driver.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder']//span[@class='quantumWizMenuPaperselectContent exportContent']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup']//span[@class='quantumWizMenuPaperselectContent exportContent' and text()='Option 4']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352