简体   繁体   English

使用 Selenium - JAVA 选择单选按钮时遇到问题

[英]Trouble selecting Radio buttons using Selenium - JAVA

I am attempting to automate some testing using Selenium on Chrome.我正在尝试在 Chrome 上使用 Selenium 自动化一些测试。 I am running into an issue with selecting radio buttons before moving onto the next step.在进入下一步之前,我遇到了选择单选按钮的问题。 I am constantly getting a 'NoSuchElementException' error with every method I try for selecting the radio button.我尝试选择单选按钮的每种方法都经常出现“NoSuchElementException”错误。 Below is the html code that is for the radio buttons, I am trying to select the first "New (Empty)".下面是用于单选按钮的 html 代码,我正在尝试 select 第一个“新(空)”。

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

Below are a few of the methods I have attempted selecting it wth (the thread sleep is in there as that was a common issue people observed with radio buttons):以下是我尝试选择它的一些方法(线程睡眠在那里,因为这是人们使用单选按钮观察到的常见问题):

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

I tried others but did not keep track of them, they all threw the same NoSuchElementException error.我尝试了其他人但没有跟踪他们,他们都抛出了相同的 NoSuchElementException 错误。

I attempted selecting it by creating a List as suggested on other thread below, for this I get an idex error since the list contain nothing:我尝试按照下面其他线程的建议通过创建一个列表来选择它,为此我收到一个 idex 错误,因为该列表不包含任何内容:

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

To click() on the element with text as New (Empty) you can use either of the following Locator Strategies :要在元素上click() ,文本为New(空) ,您可以使用以下任一Locator Strategies

  • cssSelector : cssSelector

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

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

However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :但是,由于元素是动态元素,因此要在元素上click() ,您需要为elementToBeClickable()诱导WebDriverWait ,并且您可以使用以下任一定位器策略

  • cssSelector : cssSelector

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

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

Try to create a local.html file with your provided HTML code and try your methods again on that local HTML file.尝试使用您提供的 HTML 代码创建一个 local.html 文件,然后在该本地 HTML 文件上再次尝试您的方法。 I have tried and all of your methods are working perfectly.我已经尝试过,您的所有方法都运行良好。

The issue is not in your methods or script, its something else.问题不在于您的方法或脚本,而在于其他问题。 May be those elements are dynamic or not clickable at that point.可能是这些元素是动态的或当时不可点击。

So can you provide some more details.那么您能否提供更多详细信息。 Like what is the scenario or test case, what are the previous steps to reach that point (clicking on those radio buttons).就像场景或测试用例是什么一样,达到该点的先前步骤是什么(单击那些单选按钮)。 And If possible then provide some more HTML code (parent tags) and a screenshot of your webpage.如果可能的话,请提供更多 HTML 代码(父标签)和您网页的屏幕截图。

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

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