Tips on obtaining the total element count within a specific div using Selenium

I am currently working on retrieving the count of search results when using the MakeMyTrip application to search for flights from Hyderabad to Bangalore. While I have successfully managed to extract the text, I am now looking for a way to confirm the number of search results that were returned.

Here is the code snippet I used to retrieve the text:

String output = driver.findElement(By.xpath("//*[@id=\"left-side--wrapper\"]/div[3]")).getText();

You can view an example of the MakeMyTrip Flight Search screenshot here.

System.out.println(output);

Any help or insights would be greatly appreciated. Thank you in advance.

Answer №1

To retrieve multiple elements on a webpage using Selenium WebDriver, utilize the driver.findElements(); method as demonstrated in the code snippet below:

// Target your specific web element
By searchElement = By.xpath("//*[@id='left-side--wrapper']/div[3]");

// Store all found elements in a List
List <WebElement> allSearchElements = driver.findElements(searchElement);

// Obtain the size of the List to determine the number of elements found
int numOfElements = allSearchElements.size();

System.out.println("There are " + numOfElements + " elements present on the page");

This code snippet should provide the assistance needed.

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

What could be causing issues with the functionality of specflow tags in my scenarios?

I have come up with a few scenarios in the feature file Patient Search, each having 2 different tags associated with them as shown below: @PatientSearch @Functional Scenario: Patient Search-Choose a Favorite Search Given I do not have any Portal page o ...

An InvalidArgumentException is triggered by Selenium in the event of a rotating proxy rotation

I came across this code on GitHub that is supposed to gather IPs from and rotate them. However, I encountered an error message when attempting to run it. Despite my efforts to debug the code, I couldn't pinpoint the solution. Could it be due to a co ...

extracting URLs using selenium with an array

I am working with the following code: driver = webdriver.Firefox() for element in links: driver.get(element) html = driver.page_source soup = BeautifulSoup(html, 'html.parser') #driver.switchTo().window() driver.close() ...

Error: Unable to locate the module 'selenium' when using subprocess

Experiencing an issue when attempting to launch a program with a subprocess. The program runs smoothly when executed directly, without going through another file. Python 3.10 is being used on Windows 10. I have verified that the selenium module is up to d ...

The anticipated condition was not met as we were waiting for the element located by the XPath to become visible

public class ProgramDemoQA { public static WebDriver d; public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\RamanaSoft&bso ...

The button labeled as a "span action" is hidden from view

Include an input box and a button labeled "Choose File." The form features and element descriptions are as follows: In order to locate the button, the following code was used successfully: WebElement UploadButton=driver.findElement(By.xpath("//div[@clas ...

What is the best way to create a function that allows us to "wait for ajax" to load before proceeding with the page and then easily reuse that function in other test methods

I have been experimenting with different waiting options, but I am unable to click the button. It seems like the AJAX is taking time to load the page. How can I create a function to wait for AJAX and use it in other methods when needed? for (int row = 1; ...

Instructions for setting up Saucelabs tunnelidentifier with AddAdditionalCapability in C#

Below is the code I have been working on, trying to set up the tunnel identifier and launch a test URL. Unfortunately, I haven't been successful in getting it to work with sites that are blocked by proxy. Any assistance would be greatly appreciated. T ...

Error encountered: WebDriver session not found while using ChromeDriver with Selenium and Jasmine

Currently, I am working on creating an automated test for my website using a combination of Jasmine and selenium. However, when I run the tests on chrome with chromedriver, I encounter the error below randomly. This issue occurs frequently enough that it ...

What sets DesiredCapabilities and RequiredCapabilities apart from each other?

Could anyone provide some insight into the contrast between DesiredCapabilities and RequiredCapabilities when used in the RemoteWebDriver constructor? If you need more information on the constructor, you can refer to this link: ...

Guide to creating a new browser tab with Selenium WebDriver in Java

Is there a way to launch a new tab within the current Firefox browser using Selenium WebDriver (also known as Selenium 2) in Java? ...

Activate WebDriver to open a new browser tab

After extensively searching the internet and examining the WebDriver API, I couldn't find a method to open new tabs using WebDriver/Selenium2.0. Can anyone verify if my assumption is correct? Thanks, Chris. P.S: At the moment, it seems like the only ...

How can the Hooks folder be utilized within the Cucumber framework?

Currently, I am in the process of developing a selenium web driver and cucumber framework. I recently came across a framework that has a specific folder for Hooks. Typically, most frameworks only have one hooks file. So, why is it necessary to have an en ...

What is the process for creating xpath expressions for text that is contained within <br> tags?

Struggling to craft the perfect xpath for extracting text enclosed within 'br' tags in the HTML snippet below. Despite attempting to identify the xpath using 'chropath', it returns '0 elements matching'. Seeking assistance to ...

Encountering Issues with Selenium Firefox Webdriver's Get Function while Utilizing FirefoxProfile

Encountering a frustrating issue with the Selenium Firefox webdriver crashing when making calls to the "get" function. Interestingly, the error only arises when utilizing a firefox_profile; everything functions smoothly when firefox_profile is set to None. ...

Retrieve the input field's value with Selenium, verify its accuracy, and proceed to log a message to the console

Hey there! I'm facing a challenge while working with Selenium Webdriver, specifically Chrome Webdriver and writing tests in JavaScript. The problem is in a section of the code where I can't seem to grab the value typed into an input field using t ...

Can JMeter Thread group be configured to use 2 different WDS for distinct user roles simultaneously?

In the Application, there exist two distinct user roles. When attempting to run both user roles sequentially within one browser (login1 - actions1 - logout1, login2 - actions2 - logout2), issues arise related to users' identity, page visibility, and ...

The Function parameter used in the FluentWait Method does not match the expected arguments

Encountering a problem with the until method in appium using selenium webdriver. An error is being thrown: The method until(Function) in the type FluentWait is not applicable for the arguments (new Function(){}) Tried all solutions mentioned in previou ...

Guide on evaluating the Print and Download features with Selenium WebDriver while using a Proxy

During my testing, I perform a click on the Print or Download button and then verify that the table from the UI screen has been printed/downloaded correctly. Any suggestions on how to automate this test scenario? I was thinking of possibly utilizing a pr ...

Tips for finding ng-model in a div tag using Selenium in C#

Can someone assist me in finding an element with the attribute ng-model="error-message"? This specific element is located within a div container and I am currently using Selenium with C#. <label ng-model="errorMessage" class="text-danger message_cont ...