Home > OS >  selenium loop through radio buttons python
selenium loop through radio buttons python

Time:03-15

I am trying to loop through two radio buttons click on one get some details and then click on the second one and get the same details again. Its clicking on the first one but I can't get it to click on the second.

    radio_button = wait.until(EC.presence_of_all_elements_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))
    print (radio_button)

    for radio in range(len(radio_button)):
        radio = wait.until(EC.presence_of_element_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))
        radio.click()
        "get some data"
        print (radio)

The print out of "radio_button" shows that I am getting the two different elements back that I want to click.

 <selenium.webdriver.remote.webelement.WebElement (session="dda6abe5-88a7-419a-8cb8-ff7438739c62", element="aa9d00e5-06f2-4f64-ab6a-8382f13c0cf9")>
 <selenium.webdriver.remote.webelement.WebElement (session="dda6abe5-88a7-419a-8cb8-ff7438739c62", element="70c7d3d9-5c3b-4939-a1ea-ccf1287adab5")>

The print out of "radio" shows that I am getting the first element back twice.

<selenium.webdriver.remote.webelement.WebElement (session="dda6abe5-88a7-419a-8cb8-ff7438739c62", element="aa9d00e5-06f2-4f64-ab6a-8382f13c0cf9")>
<selenium.webdriver.remote.webelement.WebElement (session="dda6abe5-88a7-419a-8cb8-ff7438739c62", element="aa9d00e5-06f2-4f64-ab6a-8382f13c0cf9")>

what am I doing wrong? Many thanks in advance.

CodePudding user response:

You are getting the first element back to back twice as everytime you call:

radio = wait.until(EC.presence_of_element_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))

in both the iteration the first matching element, i.e. the first radio button gets identified and gets clicked.


Solution

You can make a small modifications as follows:

radio_buttons = wait.until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))
print (radio_buttons)

for radio in radio_buttons :
    radio = wait.until(EC.element_to_be_clickable(radio))
    radio.click()
    print (radio)

CodePudding user response:

The reason you are getting the same element because you are iterating the list and looking for the xpath it will always returned first element only.

You need use the indexing to get the specific element.

radio_button = wait.until(EC.presence_of_all_elements_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))
print (radio_button)

for radio in range(len(radio_button)):
    radio = wait.until(EC.visibility_of_element_located((By.XPATH,"(//input[@class='vfuk-Radio__input'])[{} 1]".format(radio))))
    radio.click()
    "get some data"
    print (radio)

Or just use list[elementIndex]

radio_button = wait.until(EC.presence_of_all_elements_located((By.XPATH,"//input[@class='vfuk-Radio__input']")))
print (radio_button)

for radio in range(len(radio_button)):
    radio =radio_button[radio] 
    radio.click()
    "get some data"
    print (radio)
  • Related