Home > front end >  How to click on a radiobutton with respect to the text using Selenium and Python
How to click on a radiobutton with respect to the text using Selenium and Python

Time:01-17

Can you help me locate this radiobutton with the help of Selenium(Python) located on a page with a rather strange structure at "https://www.avans.pl/reklamacja"

enter image description here

location of the element on the page with the possibility of using the Click() function on it.

Code trials:

import datetime
import time

# from selenium import webdriver
# from selenium.webdriver.common import keys
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.select import Select
# from webdriver_manager.chrome import ChromeDriverManager

from selenium.webdriver.support.select import Select
from selenium.webdriver.common import keys
from selenium.webdriver.common.by import By
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver import Firefox

driver = Firefox(executable_path=GeckoDriverManager().install())

def MailAdrCreator():
    datadzis = datetime.date.today()
    tab_date = str(datadzis).split("-")
    adr = tab_date[0] tab_date[1] tab_date[2]
    email = "test.formularzy.terg " adr "@gmail.com"
    return email

driver.get("https://www.avans.pl/reklamacja")
driver.maximize_window()
driver.find_element_by_id("cc_form_1").send_keys("Jan")
driver.find_element_by_id("cc_form_3").send_keys("Kowalski")
driver.find_element_by_id("cc_form_36").send_keys(MailAdrCreator())
driver.find_element_by_id("cc_form_841").send_keys("02321484339")
driver.find_element_by_id("cc_form_843").send_keys("pralka, lodówka, radio")
driver.find_element_by_id("cc_form_363").send_keys("ABC12962019621")
driver.find_element_by_id("cc_form_1292_0").click()
# driver.find_element_by_id("cc_form_1292_1").click()
# time.sleep(5)
driver.close()

CodePudding user response:

Try the below code:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
# to accept the cookies information
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".c-alert_close"))).click()

# scroll to the title of the page to view the 'Tak' radio button
driver.execute_script("arguments[0].scrollIntoView(true)", driver.find_element(By.CSS_SELECTOR, ".c-headline_title.a-typo.is-primary"))

radio_btn = driver.find_element(By.CSS_SELECTOR, ".a-form_radio.is-customCheckbox_label #cc_form_1292_0")
# clicking on the radio button using javascript
driver.execute_script("arguments[0].click();", radio_btn)

CodePudding user response:

To click on the associated with either of the text Tak / Nie you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Clicking on Tak:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-synerise='Tak']//parent::label[@class='a-form_radio is-customCheckbox_label']"))).click()
    
  • Clicking on Nie:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-synerise='Nie']//parent::label[@class='a-form_radio is-customCheckbox_label']"))).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
    
  • Related