Home > Software engineering >  How to select a radio button with Selenium?
How to select a radio button with Selenium?

Time:07-21

HTML:

<input id="rbtn50" type="radio" name="rbtn50" value="rbtn50" onclick="javascript:setTimeout('__doPostBack(\'rbtn50\',\'\')', 0)">

Current radio button at

<input id="rbtn20" type="radio" name="rbtn20" value="rbtn20" checked="checked">

Attempt 1:

button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "rbtn50")))
button.click()

Attempt 1:

button = driver.find_element(By.ID, 'rbtn50')
button.click()

Error:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="rbtn50"]"}

CodePudding user response:

You could try using an xpath for it

button = driver.find_element(By.XPATH, "//input[@id='rbtn50']")
button.click()

CodePudding user response:

Check if this element was loaded inside an iframe element, maybe the xpath from the element that you want is being searched in the outerMost parent element, but the Xpath is in fact inside the element of the second html.

Solution, use: wdriver.switch_to.frame('id_of_the_iframe_you_want_to_inspect')

If this is the case: This function switches the focus to a specific frame. you can pass other parameters to identify the iframe you must target to SELENIUM to inspect and find your element.

If your element is inside the iframe, find_element won't work until you switch the focus of it.

  • Related