Home > Net >  Selenium Radio Button not Functioning
Selenium Radio Button not Functioning

Time:05-17

I wish to create a bot for automatically fill in Google Form survey, which is filled with radio buttons. When I am trying to fill in the form, the first Gender radio button was successfully clicked. However, the second age group button was not? Is this a bug?

import time
from selenium import webdriver



browser = webdriver.Chrome(executable_path='C:/Users/ryan/Downloads/chromedriver.exe')
browser.get('https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBB/viewform?fbzx=-1760864547538697754')

RadioGender= browser.find_element_by_xpath('//*[@id="i5"]/div[3]/div')
RadioGender.click()

RadioAge= browser.find_element_by_xpath('//*[@id="i18"]/div[3]/div/div')
RadioAge.click()

CodePudding user response:

To handle dynamic element use WebDriverWait() and wait for element to be clickable and following locator strategy.

browser.get("https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBBt1MqEgoQOz9oHWWmTY1Q/viewform?fbzx=-1760864547538697754")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Next"]'))).click()
RadioGender= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@data-value="Male"]')))
RadioGender.click()

RadioAge=  WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@data-value="21-23"]')))
RadioAge.click()

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Note: If you would like to select other age range, you need pass those values as data value.

  • Related