简体   繁体   English

无法选择硒中单选按钮的第二个选项

[英]Unable to select second option of radio button in selenium

This is what is already selected: 这是已经选择的内容:

<input type="radio" tabindex="0" name="accounts" onclick="populateSelection();" value="MC Credit ***388.37***" checked="">

CSS-selector: body > form > span > table > tbody > tr:nth-child(5) > td.walletDispaly > input[type="radio"] CSS选择器: body > form > span > table > tbody > tr:nth-child(5) > td.walletDispaly > input[type="radio"]

xpath: /html/body/form/span/table/tbody/tr[4]/td[1]/input xpath: /html/body/form/span/table/tbody/tr[4]/td[1]/input

This is what i want to select: 这就是我要选择的:

<input type="radio" tabindex="1" name="accounts" onclick="populateSelection();" value="Savings ***388.37***">

CSS-selector: body > form > span > table > tbody > tr:nth-child(7) > td.walletDispaly > input[type="radio"] CSS选择器: body > form > span > table > tbody > tr:nth-child(7) > td.walletDispaly > input[type="radio"]

xpath: /html/body/form/span/table/tbody/tr[5]/td[1]/input xpath: /html/body/form/span/table/tbody/tr[5]/td[1]/input

What I tried till now: 到目前为止我尝试过的是:

  1. driver.findElement(By.xcode("//input[@name='accounts'][2]"));
  2. driver.findElement(By.xcode("//input[@type='radio'][@name='accounts'][position()=2]"));
  3. driver.findElement(By.id("accounts"))
  4. driver.findElement(By.name("accounts"))
  5. driver.findElement(By.cssSelector("[tabindex='1']")

Why xpath are not working : 为什么xpath无法正常工作:

  1. //input[@name='accounts'][2]
  2. //input[@type='radio'][@name='accounts'][position()=2]

    If both of you element has sibling relation then only it works. 如果你们两个元素都有同级关系,那么只有它起作用。
    eg 例如

     <div> <input type="radio" tabindex="0" name="accounts" onclick="populateSelection();" value="MC Credit ***388.37***" checked=""> <input type="radio" tabindex="1" name="accounts" onclick="populateSelection();" value="Savings ***388.37***"> <div> 

    But it seems that you don't have the element in this fashion. 但似乎您没有这种方式的元素。

  3. driver.findElement(By.id("accounts"))

    There is no id attribute in your element so it won't work. 您的元素中没有id属性,因此它将无法正常工作。

  4. By.name("accounts")

  5. By.cssSelector("[tabindex='1']")

    With the same attribute there are multiple element on your page so it is doing action on very first element and you are not seeing it for your intended elements. 具有相同属性的页面上有多个元素,因此它在第一个元素上执行操作,而对于预期的元素则看不到它。 Could be the same reason for 5th one. 可能与第五名相同。

Solution : 解决方案

Make sure you are locating the element uniquely. 确保您唯一地定位元素。 You need to customize you xpath. 您需要自定义xpath。 Try below solutions : 请尝试以下解决方案:

By.xpath By.xpath

  • //input[contains(@onclick,'populateSelection')][contains( @value,'Savings')]

OR 要么

  • //input[@tabindex='1'][@name='accounts']

Instead of using absolute xpath use relative xpath . 而不是使用绝对xpath,而是使用相对xpath For the HTML as: 对于HTML为:

<input type="radio" tabindex="1" name="accounts" onclick="populateSelection();" value="Savings ***388.37***">

You can use either of the following solutions: 您可以使用以下解决方案之一:

  • cssSelector : cssSelector

     driver.findElement(By.cssSelector("input[name='accounts'][value^='Savings']")).click(); 
  • xpath : xpath

     driver.findElement(By.xpath("//input[@name='accounts' and starts-with(@value,'Savings')]")).click(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM