繁体   English   中英

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

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

这是已经选择的内容:

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

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

这就是我要选择的:

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

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

到目前为止我尝试过的是:

  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']")

为什么xpath无法正常工作:

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

    如果你们两个元素都有同级关系,那么只有它起作用。
    例如

     <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> 

    但似乎您没有这种方式的元素。

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

    您的元素中没有id属性,因此它将无法正常工作。

  4. By.name("accounts")

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

    具有相同属性的页面上有多个元素,因此它在第一个元素上执行操作,而对于预期的元素则看不到它。 可能与第五名相同。

解决方案

确保您唯一地定位元素。 您需要自定义xpath。 请尝试以下解决方案:

By.xpath

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

要么

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

而不是使用绝对xpath,而是使用相对xpath 对于HTML为:

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

您可以使用以下解决方案之一:

  • cssSelector

     driver.findElement(By.cssSelector("input[name='accounts'][value^='Savings']")).click(); 
  • 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