选择具有相同名称且没有id的任意单选按钮,selenium webdriver Python

时间:2016-02-09 00:09:52

标签: jquery python html selenium selenium-webdriver

我试图从单选按钮列表中选择,并选择我想要的任何项目。我试过了

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

问题是它总会选择第一项。唯一的区别是订单和每个单选按钮的值设置:

<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

如何在此处选择任意单选按钮?谢谢

对不起,我刚注意到我确实有足够的逻辑:用//input[@value...注意xpath

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    

1 个答案:

答案 0 :(得分:1)

您的代码看起来没有问题,您可以试试这个:

List<AnimalBuild> animalList  = new ArrayList<AnimalBuild>();
Object obj = null;
while ((obj = ois.readObject()) != null) {
    if (obj instanceof AnimalBuild) {
    AnimalBuild ab = (AnimalBuild) obj;
    animalList .add(ab);
    }
}