0

I am creating a test for clicking a radio button by clicking "Yes"

radio button code:


<div role="group" class="btn-group btn-group-toggle">
 <label class="btn btn-primary">
 <input name=".product2" autocomplete="off" value="true" type="radio"> Yes </label>

i already tried:

time.sleep(3)
driver.find_element_by_css_selector("input[name*='product2'] [value='true']").click();

but it returns an error:

Element <input name=".product2" type="radio"> could not be scrolled into view

Is there any other selector that i can use to click "Yes"? or is there a missing details using css selector?

I also tried find_element_by xpath by still have the same result.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • if the element is not visible in current window, then first you need to scroll to that location and then you can select the radio button. here is possible solution https://stackoverflow.com/a/41744403/2845389 – Kaushik May 07 '19 at 04:36

2 Answers2

2

You can try with this css selector :

label.btn.btn-primary input  

It would be good if you could use WebDriverWait as well.

wait = WebDriverWait(driver, 10)

element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.btn.btn-primary input")))

element.click()  

Approach 2 :

or you can try with ActionChains also :

actions = ActionChains(driver)
wait = WebDriverWait(driver, 10)

element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.btn.btn-primary input")))
actions.move_to_element(element).perform()

element.click()

Imports would be :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains  

Hope this helps

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hi! this is working thanks :) but with a followup, there is a multiple ```class label``` that were constructed with exactly the same radio button and the only different is the ```name``` . how can I click the specific radio button that I want to click using ```name```? is that possible with css selector? for example: 3 radio button with same syntax and the only different is ```name``` ```.product1``` , ```.product2``` , ```.product3``` how can click the 2nd button ```name='.product2'```? – Quentin Richarson May 07 '19 at 21:21
  • 1
    yes, it's possible with css selector , `label.btn.btn-primary input[name='.product1']` , you can change name attribute like that. If this answer is helpful to you, Please give me credit by clicking on check mark which is just besides my answer. – cruisepandey May 08 '19 at 04:48
0

@cruisepandey answer seems correct, if it works it is good but if stills not work you can use javascriptexecutor

element=driver.find_element_by_css_selector("input[name*='product2'] [value='true']") 
driver.execute_script("arguments[0].click();", element)
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36