Home > Mobile >  How to click radio button with selenium in python?
How to click radio button with selenium in python?

Time:08-17

I want to select a radio button with selenium in python. I have tested with 3 solutions. Unfortunately it doesn't work at all. Could you do me a favor and help me. I am totally beginner. The URL: enter image description here

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('C:\Webdriver\chromedriver.exe')
driver.get('https://biruni.tuik.gov.tr/disticaretapp/menu_ing.zul')
time.sleep(2)
driver.maximize_window()
element = driver.find_element(by=By.XPATH,value='//*[contains(text(), "Product/Product Groups-Partner Country")]')                                                                                                            
element.click()
time.sleep(4)

# radio = driver.find_element_by_id("o1BQ41-real")
# radio.click()


# l=driver.find_element_by_xpath('//*[@id="o1BQ41-real"]')
# l.click()


# driver.find_element_by_css_selector("input#o1BQ41-real").click() 

time.sleep(10)

CodePudding user response:

You can use the same select by text to click on the radio button.

radio_element = driver.find_element(by=By.XPATH,value='//*[contains(text(), "Product/Partner Country")]')
radio_element.click()

This selects the desired element

enter image description here

or you can also select the element by id

radio_element = driver.find_element(by=By.XPATH,value='//span[@id="bKFP41"]')
radio_element.click()

CodePudding user response:

That should solve your problem:

element = driver.find_element(by=By.XPATH,value='//html/body/div/div/div/table/tbody/tr/td/table/tbody/tr[3]/td/div/div/table/tbody[1]/tr[13]/td/div/span/span[2]/input')                                                                                                            
element.click()

You can't use the element id in XPATH because every refresh the site changes the element id!

  • Related