The Issue

Trying to find and interact with the correct element can be a frustrating process. A common issue is trying to execute a Selenium or Watir Webdriver command on an element that shares the identifier you are using with other elements. Determining if a specific identifier is shared is important to troubleshooting your automated test script. How can we best determine this?

The Answer

We can check the count of elements with a shared identifier using the following Watir Webdriver  command:

We would replace elements and text with our respective identifiers. We require a plural element type when using the .count command. After entering the command, we are returned a value that indicates the element count on our page.

The Code

We can use the 3Qi Labs homepage for our example here:

The Result

The multiple 'Learn More' buttons that our Watir-Webdriver command captured.

The multiple ‘Learn More’ buttons that our Watir-Webdriver command captured.

We save and run the automated test script (ElementCount.rb) and it runs the following:

  • Browser opens
  • Navigates to 3Qi Labs homepage
  • Checks for count of Learn More link
  • Count output is displayed

Here we see the count output displayed:

111Capture

 

 

The Takeaway

You can now implement the .count command into your automated test scripts or debugging process. Using Selenium & Watir you can help troubleshoot an issue with element interaction, or simply confirm that the correct number of specified elements are displaying.