I cannot find the XPath or the class name for the radio button which should select color randomly from the list using selenium in a google form(link). Other name, age, country, and email are working fine with the XPath. I want the code to select any of the four colors randomly and automatically, I don't want it to wait for me to select a color. Every time getting an error comes up. Can anyone help me? Code is -
from selenium import webdriver
from faker import Faker
import random
driver = webdriver.Chrome()
driver.get("https://forms.gle/PL3W5TxZVVJHRa1W9")
faker = Faker()
colors = ["green", "blue", "black", "red"]
for i in range(20):
name = faker.name()
age = random.randint(18, 99)
country = faker.country()
email = faker.email()
color = random.choice(colors)
name_field = driver.find_element('xpath','//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
name_field.send_keys(name)
age_field = driver.find_element('xpath','//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input')
age_field.send_keys(str(age))
country_field = driver.find_element('xpath','//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input')
country_field.send_keys(country)
email_field = driver.find_element('xpath','//*[@id="mG61Hd"]/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div/div[1]/input')
email_field.send_keys(email)
color_field = driver.find_element("class name",'//div[@]')
color_field.send_keys(color)
submit_button = driver.find_element_by_css_selector("[type='submit']")
submit_button.click()
driver.quit()
CodePudding user response:
The desired element is a dynamic element, so to click on the clickable element associated with any of the random color from the list and substituting using f-strings, you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
Using XPATH:
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSc5v43tKVcCl_a-kRW8jijoUuK-O4hR2CYNK-CDMRe8NulXog/viewform") # WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='green']"))).click() colors = ["green", "blue", "black", "red"] color = random.choice(colors) print(color) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, f"//span[text()='{color}']"))).click()
Console Output:
green
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
Browser Snapshot:
CodePudding user response:
All the 4 elements choosing the color having the same locator //div[contains(@data-params,'color')]//label
. So, you can choose one of them randomly.
The following code works:
import random
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://docs.google.com/forms/d/e/1FAIpQLSc5v43tKVcCl_a-kRW8jijoUuK-O4hR2CYNK-CDMRe8NulXog/viewform"
driver.get(url)
color_index = random.randint(1, 4)
random_color_locator = "(//div[contains(@data-params,'color')]//label)[{}]".format(color_index)
wait.until(EC.element_to_be_clickable((By.XPATH, random_color_locator))).click()