Locating and selecting a boolean checkbox with Selenium and Java: A step-by-step guide

I'm currently attempting to click a primefaces select boolean checkbox using the ID, but I'm encountering an issue. The exception message states:

org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.id: runDARtest1

I've tried increasing the timeout to 30 seconds, but the problem persists.

driver.get("http://localhost:5080/MOIA2-WEB/");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:userName")).sendKeys("cherie.chen");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:password")).sendKeys("Password1");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:loginButton")).click();
driver.findElement(By.id("moia2Form:lnkMarketClearing")).click();
driver.findElement(By.id("moia2Form:system_Result_runinfo")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("runDARtest1"))).click();;

The Xhtml code has a select boolean checkbox within a table structure:

<tr>
    <td><label class="switch"> <h:selectBooleanCheckbox
                                            class="runDARclass"
                                            value="#{systemResultManagedBean.runDAR}" id="runDAR" />
    <span class="slider round" id="runDARtest1"></span>
    </label></td>
    <td><p:spacer width="5px" /><span>DAR</span></td>
</tr>

Answer №1

If you need to interact with a dynamic element, make sure to use click() after waiting for the element to be clickable using WebDriverWait. Here are two options you can consider:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.switch span.slider.round#runDARtest1"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='switch']//span[@class='slider round' and @id='runDARtest1']"))).click();
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When parsing a JSON string into a JSONArray object, any trailing decimal point with a

My JSON string contains floating values: {"foo":10.0,"bar":12.005} I am trying to convert it using the method JSONObject(jsonstring), but the resulting JSON array drops decimal values that are zero, showing like this: {"foo":10,"bar":12.005} ...

How do I ensure in Selenium using Java that all backend processes have finished before moving on to the next command?

I have multiple backend processes running in my project, causing some of my test cases to pass intermittently. Is there a method in Selenium that allows me to wait for all backend processes to complete before executing the next command? ...

Is there a glitch in the Selenium Java CSS Selector functionality?

Everything seems to be working smoothly with that code! It successfully locates and clicks on my button within the span tag. driver.findElement(By.cssSelector("span[id$=somePagesCollection] a")).click(); However, after clicking the button, an input field ...

Steps for displaying custom element name from Page Object Model in test report log

To accurately track user interactions, I require the WebElement name (User-defined) for reporting purposes. When performing a Click operation on AddMainConcernLink, I need to report whether the element was successfully clicked or not as "AddMainConcernLink ...

Populate the JTable with SparQL query results in JSON string format

Trying to populate a jtable with json data, I experimented with various solutions such as: String json = " ObjectMapper mapper = new ObjectMapper(); List<User> users = mapper.readValue(json,TypeFactory.defaultInstance().constructCollectionType(List ...

Using Modernizr to determine the presence of nth-child in a test

I'm attempting to utilize modernizr in order to check for browser support of the :nth-child selector, but I am unsure of how to proceed. I came across this particular example on http://jsfiddle.net/laustdeleuran/3rEVe/ which tests for :last-child. How ...

Ways to verify that each item in the list includes at least one character from a specified string

List<WebElement> prices = driver.findElements(By.className("price")); for (WebElement price : prices) { System.out.println(price.getText()); } String currencySymbol = driver.findElement(By.cssSelector(" ...

Executing UI automation with Selenium and Java involves the automation of two separate Chrome browsers simultaneously

I currently have 3 classes structured like this: `public class SubmitOrderTest extends DefaultTest { @Test public void submitOrder() { LandingPage landingPage = launchWebsite(); landingPage.login("standard_user", "secret_sauce" ...

Step-by-step guide on retrieving particular files from a webpage with Python and Selenium

I am looking to download specific files from this particular page. The exact four files I need are: What is the best way to navigate through the page using Selenium while maintaining good programming practices? Are there any libraries that might be more ...

Retrieve all valid JSON data from a string in Java

There's a scenario I'm dealing with where JSON strings are separated by commas. Here's an example string: {Valid JSON String},{Valid JSON String},{Valid JSON String} I need to extract each valid JSON string and save them in an ArrayList. ...

Unable to input new data into the fields within the text box

I am facing an issue while trying to input new fields in the Origin Place section of a Flight search application at . Despite attempting various XPath and locators, I am unable to successfully input any new information. The default value in Origin Place p ...

Press the "show additional content" button on the nytimes.com website using selenium

I'm experiencing difficulty scrolling through this webpage. Once I reach the bottom of the page, I am unable to locate and click on the "SHOW MORE" button using selenium. self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") ...

Capturing the page source in Selenium prior to running any JavaScript code

As I face a situation where the raw page source code retrieved from an HTTP request includes important JSON data that needs to be extracted - for instance, <code id="data">{'some':'json'}</code>. However, the JavaScript runn ...

Check for the presence of incorrect information to verify its visibility

My current task involves writing a test in Selenium where I have designated all my web elements in a page object package. To access the web element in my test, I am utilizing S.getColumn(). The getcolumn() function retrieves the web element of the column ...

What strategies can I employ to prevent conflicts while executing Selenium tests concurrently, especially when they need to interact with a REST API?

Looking for a way to test my web application in various browsers and environments, such as Chrome, Firefox, and Internet Explorer on Windows and Linux (except for Internet Explorer). The tests have been developed in Java using JBehave, Selenium, and Seren ...

Python: Step-by-step guide for completing a web form and retrieving the corresponding page source

My goal is to develop a python script that can analyze news articles from and determine if they are fake or real news. The script should input the news article into the website's url field and click the submit button. Then, by scraping the site, it w ...

The ElementNotInteractableException from selenium.common.exceptions was triggered because the element was unresponsive when attempting to input text

Having trouble entering text into the email field on . I tried using WebDriverWait but it didn't work. Any help is appreciated. import time import datetime import random from selenium.webdriver.chrome.options import Options from selenium import webdri ...

Learn how to execute JavaScript code in Selenium without launching a web browser

I am currently using the Selenium API for web scraping on pages that contain JavaScript code. Is there a way to retrieve the code without having to open a web browser? I am still learning how to use this API Is this even feasible? ...

The execution of Python scripts has ceased

I've been experiencing an issue with running certain Python scripts lately. The message 'Ran 0 tests in 0.000s' keeps popping up unexpectedly. It's strange because some of the scripts are still working fine, but others just won't ...

Having difficulty extracting table data with selenium and beautiful soup

I've hit a wall in trying to extract data from a table on Yahoo's daily fantasy webpage. Despite scouring StackOverflow for solutions, I can't seem to get the desired information. The table either appears empty or the elements within it are ...