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