【发布时间】: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