【问题标题】:Trouble selecting Radio buttons using Selenium - JAVA使用 Selenium 选择单选按钮时遇到问题 - JAVA
【发布时间】:2020-08-17 18:07:56
【问题描述】:

我正在尝试在 Chrome 上使用 Selenium 自动执行一些测试。在进行下一步之前,我遇到了选择单选按钮的问题。我尝试选择单选按钮的每种方法都经常出现“NoSuchElementException”错误。下面是单选按钮的 html 代码,我正在尝试选择第一个“新建(空)”。

<td>
    <input type="radio" name="selections" value="emptyAssembly" id="New (Empty)" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], emptyAssembly)">
    New (Empty)
    <br>
    <input type="radio" name="selections" value="existingAssembly" id="Existing Template, Assembly or View" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], existingAssembly)">
    Existing Template, Assembly or View
    <br>
    <input type="radio" name="selections" value="assemblyFile" id="Assembly File" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], assemblyFile)">
    Assembly File
    <br>
    <input type="radio" name="selections" value="virtualDocument" id="Virtual Document" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], virtualDocument)">
    Virtual Document
    <br>
</td>

以下是我尝试选择它的一些方法(线程睡眠在那里,因为这是人们使用单选按钮观察到的常见问题):

Thread.sleep(5000);
webDriver.findElement(By.xpath("//input[@id='New (Empty)']")).click();
Thread.sleep(5000);
webDriver.findElement(By.id("New (Empty)")).click();

我尝试了其他人但没有跟踪他们,他们都抛出了相同的 NoSuchElementException 错误。

我尝试按照下面其他线程的建议通过创建一个列表来选择它,为此我收到一个 idex 错误,因为该列表不包含任何内容:

webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = webDriver.findElements(By.name("selections"));
methods.get(0).click();

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    对于文本为 New (Empty) 的元素上的 click(),您可以使用以下任一 Locator Strategies

    • cssSelector:

      webDriver.findElement(By.cssSelector("input[id*='Empty'][value='emptyAssembly']")).click();
      
    • xpath:

      webDriver.findElement(By.xpath("//input[@value='emptyAssembly' and contains(@id, 'Empty')]")).click();
      

    但是,由于元素是动态元素,因此要在元素上使用click(),您需要为elementToBeClickable() 诱导WebDriverWait,并且您可以使用以下任一定位器策略

    • cssSelector:

      new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id*='Empty'][value='emptyAssembly']"))).click();
      
    • xpath:

      new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='emptyAssembly' and contains(@id, 'Empty')]"))).click();
      

    【讨论】:

    • 感谢您添加最佳实践类型项目。事实证明,我实际遇到的问题根本与此无关,因为我不知道 html 编码中存在框架,并且我需要在其中处于活动状态才能使用其中的字段。我将在我的代码中添加 WebDriverWait 时间。
    【解决方案2】:

    尝试使用您提供的 HTML 代码创建一个本地 .html 文件,然后在该本地 HTML 文件上再次尝试您的方法。我试过了,你所有的方法都很好用。

    问题不在于您的方法或脚本,而在于其他问题。可能是这些元素是动态的或当时不可点击。

    您能否提供更多详细信息。就像场景或测试用例是什么一样,达到该点的先前步骤是什么(单击那些单选按钮)。如果可能,请提供更多 HTML 代码(父标签)和网页截图。

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 2014-12-08
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多