codyc4321 February 2016

pick arbitrary radio button with same name and no id, selenium webdriver Python

I am trying to pick from a list of radio buttons, and pick whichever item I want. I tried

ele = driver.cl.driver.find_element_by_name("samename")
ele.click()

and the issue is it will always pick the first item. The only differentiation is the order, and the value setting for each radio button:

<label>
<input type="radio" name="samename" value="one">first
</label>
<label>
<input type="radio" name="samename" value="two">second
</label>
<label>
<input type="radio" name="samename" value="three">third

How can I pick an arbitrary radio button here? Thank you

Excuse me, I just noticed I did have logic that suffices: notice the xpath using //input[@value...

def locate_element(self, search_text, xpaths=None):
    if not xpaths:
        xpaths = ["//button[normalize-space(text())='{text}']", "//input[@value='{text}']",
                  "//a[child::span[normalize-space(text())='{text}']]", "//a[normalize-space(text())='{text}']"]
    try:
        return self.driver.find_element_by_id(search_text)
    except:
        try:
            return self.driver.find_element_by_name(search_text)
        except:
            for path in xpaths:
                try:
                    return self.driver.find_element_by_xpath(path.format(text=search_text))
                except:
                    pass
    return None    

Answers


Buaban February 2016

It looks like there is no problem in your code, you can try this:

twoElement = driver.cl.locate_element("two")
twoElement.click()

Post Status

Asked in February 2016
Viewed 1,833 times
Voted 10
Answered 1 times

Search




Leave an answer