Having difficulty in choosing and displaying the dropdown values

Below is the code snippet:

        WebElement pooldropdown=driver.findElement(By.xpath("/html/body/section/section[2]/div[1]/select"));
        Select sel = new Select(pooldropdown);
        List<WebElement> list =  sel.getOptions();
        System.out.println("Number of Pool items : "+list.size()); 

          for(int i =0; i>list.size() ; i++){
          System.out.println(list .get(i).getText());

          }

This is the HTML Code provided:

<section id="content-data"><div class="cpoolSelection" id="poolSelection"><div class="cpoolLabel" id="poolLabel">Select Pool : </div><select onchange="callSetPoolId()" id="poolFilter" style="display: none;"><option value="0"> All </option><option selected="true" value="1">National</option><option value="2">Special Reserve</option><option value="3">NAT_PERM</option><option value="4">NE_PERM</option><option value="5">SE_PERM</option><option value="6">NC_PERM</option><option value="7">SC_PERM</option><option value="8">SCNW_PERM</option><option value="9">NC4C_PERM</option><option value="10">RSC_PERM</option><option value="11">A&amp;D_PERM</option><option value="12">FCP</option><option value="13">R&amp;D</option><option value="14">Support</option><option value="15">Other</option><option value="17">RSA Lease</option></select><button class="ui-multiselect ui-widget ui-state-default ui-corner-all poolClass ui-state-active" type="button" aria-haspopup="true" style="width: 228px;">
<span class="ui-icon ui-icon-triangle-1-s"></span>
<span>National</span>
</button>
</div>

I'm encountering issues with selecting the drop-down values using the Selenium code provided above. Can someone assist me in selecting values from the drop-down and validating all the options present?

Answer №1

Can you help me make my for loop code better? :

for(int i = 0; i > list.size() ; i++){
          System.out.println(list .get(i).getText());

          }

change the code to:

for(int i = 0; i < list.size() ; i++){ //please note the sign change
          System.out.println(list .get(i).getText());

          }

Answer №2

Two key tasks are involved in your query:
1. Choosing values from a dropdown menu
2. Verifying options

WebElement poolDropDown = driver.findElement(By.id("cpoolLabel"));
Select select = new Select(poolDropDown);
// Different methods to choose values from the dropdown
select.selectByValue(“10”);
select.selectByIndex(2);
select.selectByVisibleText(“SCNW_PERM”);

List<WebElement> optionsList = select.getOptions();
System.out.println("Total number of items in Pool: "+optionsList.size()); 

for(int i=0; i<optionsList.size(); i++){
    System.out.println(optionsList.get(i).getText());
}

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

Utilizing Asynchronous Functions within a Loop

I have a situation where I am working with an array and need to make API calls for each index of the array. Once the API call is resolved, I need to append the result in that specific element. My goal is to ultimately return the updated array once this pro ...

The Navbar in my React Material UI app is being covered by the Drawer component. Can someone guide me on how to fix

I am facing an issue where the drawer is overlaying my navbar instead of disappearing behind it when opened. I tried adjusting the z-index in my styles but it doesn't seem to be working as expected (see screenshot). The z-index for the navbar is set h ...

Utilizing jQuery to send an Ajax GET request for a JSON file

Recently I have begun delving into the world of jQuery and Ajax, attempting to utilize both technologies to retrieve a JSON FILE. Below is the structure of the file: [ { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": f ...

Tips for generating JavaScript within list elements using a PHP script

My PHP script is designed to fetch data from a database and display it as list items within a jQuery Dialog. The process involves creating an array in the PHP while loop that handles the query results, with each list item containing data, an HTML button, a ...

Is there a way to set up gulp without relying on npm when using shared hosting?

While working on my shared hosting, I encountered a roadblock regarding the installation of gulp for compiling LESS assets into CSS. The hosting administrator has declined to install npm, which is essential for setting up gulp. Given this limitation, I am ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

Bring your Electronic Catalogue to life with the addition of dynamic HTML content showcasing the latest

I am creating a digital magazine but I am facing the challenge of having to deal with 200 pages in jpg format. To streamline the process, I am looking for a way to use a combination of JavaScript and PHP to avoid manually coding for each of the 200 pages. ...

I recently incorporated Puppeteer along with its necessary dependencies onto Heroku, and as a result, pushing my small app to Heroku now takes approximately 5 to 6 minutes

I am currently using Puppeteer to create a PDF from an HTML page. I installed the npm package by running the following command: npm i puppeteer However, when I deployed to Heroku, I encountered an error: Error while loading shared libraries: libnss3.s ...

When using Spring Boot and JUnit together with Selenium, an error may occur due to the ClassNotFoundException for the org

I'm facing an issue while attempting to execute a simple test using selenium. When I run the test, I encounter a ClassNotFoundException and I'm unsure about which dependency needs to be imported in order to resolve this problem. Despite my effor ...

What are the steps to retrieve information from your personal server using Astro?

I have successfully set up a NodeJS server using Express and Astro for the frontend, with SSR configured through the Astro adapter for NodeJS. My current challenge is fetching data from the backend, and I am unsure of the correct approach to do so. Below ...

Showcasing ranges from various input types on a single page

I have a challenge in displaying the values of multiple sliders on my webpage. Here's the code snippet I've been working on: var i = 0; var st = 'slider'; var ot = 'output'; var s = ''; var o = ''; for ...

Searching for a value within an array of objects using JavaScript: The ultimate guide

Similar Question: Locate specific object by id within a JavaScript objects array What is the best way to determine if a value exists in a given JavaScript array? For instance: var arr = [ {id: 1, color: 'blue'}, {id: 2, colo ...

Dropdown selection for countries that dynamically updates region choices

Looking to implement some JavaScript, preferably using jQuery, to create a cascading dropdown menu. Initially displaying a list of countries and upon selection, the corresponding regions for that country will be displayed in another dropdown. I assume an ...

Tips for refreshing extensive JSON structures?

I receive product data from the server in JSON format, containing properties and nested arrays up to 4 levels deep. In the frontend, users can update values within these nested structures. Should I keep track of the path and reconstruct the entire JSON obj ...

What causes the other array to be affected when one is changed?

Take a look at this snippet of JavaScript code: var x = [1, 2, 3], y = x; y[1] = 3; x; // x === [1, 3, 3] Why does this happen? Why is the value of "x" changing when I update "y[1]"? I tried it in both Firefox and Chrome and got the same result. Th ...

Caution: [Unchecked] Be mindful of using unchecked method invocation when creating a custom Selenium ExpectedCondition

Attempting to develop a bespoke Selenium explicit wait using the code snippet below: class TabsOpened implements ExpectedCondition { int expectedTabs; public TabsOpened(int exp) { this.expectedTabs = exp; } @Override public Boolean apply(Obj ...

When utilizing jQuery version 1.10.2, the HTML markup shows up prior to the page completing its loading process

I am trying to import one HTML page into another, but when I load or refresh the page, the HTML markup appears before the page fully loads. How can I fix this issue? <head> <script type="text/javascript" src="js/jquery-1.10.2.js" ></scr ...

Is it possible to dynamically assign and call functions through an Object in Angular 6?

I implemented a click handler for a button that is generated dynamically. Within the click handler function, I am invoking a callback function. However, I encountered an issue where an error message popped up stating that "Callback function is not a fu ...

Is it possible to create Firebase real-time database triggers in files other than index.js?

Is it possible to split a large index.js file into multiple files in order to better organize the code? Specifically, can Firebase triggers be written in separate JavaScript files? If so, could you please provide guidance on how to do this properly? child. ...

Updating database with Ajax when the button is clicked

Seeking guidance with a project as I am still grasping the concepts of javascript and jquery. The goal is to update a database entry upon clicking a button, where the content of the button is fetched from the database. Initial step involves querying the d ...