1

I am attempting to automate some testing using Selenium on Chrome. 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. Below is the html code that is for the radio buttons, I am trying to select the first "New (Empty)".

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

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:

webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = webDriver.findElements(By.name("selections"));
methods.get(0).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

To click() on the element with text as New (Empty) you can use either of the following 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();
    

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:

  • 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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for adding in the best practice type items. The issue I was actually running into did not have to do with this at all it turns out, it was that I was unaware that frames existed in html coding and that I would need to be active within one to use fields within it. I will be adding the WebDriverWait times throughout my code. – Michael Bunalski Aug 17 '20 at 21:46
0

Try to create a local .html file with your provided HTML code and try your methods again on that local HTML file. 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.

Rahul_Rdr
  • 13
  • 7