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

Which API is utilized by duojs for its JavaScript modules?

I am currently utilizing duojs, a front-end web development tool similar to browserify or component. With duojs, you can directly import css and js from the file itself without any external package manifests. While I'm trying to figure out how to wri ...

Ways to have a function return a promise from the final "then" in a series of promises

I am delving into the world of test automation with Selenium and JavaScript. As a newcomer to both, along with functional programming and promises, I am facing a challenge in creating a function that performs three essential tasks: Click on an input Clea ...

mootools.floor dispose function malfunctioned

I am attempting to implement form validation with the following code: However, it does not seem to be working properly. <form name="niceform" id="third" action="" class="niceform" method="post" enctype="multipart/form-data"> <div class ...

Learn a method to submit a form in a new tab by utilizing the execute_script function

browser.get('https://www.testsite.com') browser.execute_script("arguments[0].click(), 'new_tab'", browser.find_element_by_xpath('//*[@id="submitButton"]')) This piece of code triggers a new tab to open on specific websites. H ...

Angular is reporting that the check-in component is nonexistent

I encountered an error in my Angular 8 application while working on a component. The error seems to be related to nested components within the main component. It appears that if the component is empty, the error will be shown, but if it's not null, th ...

Display a hidden div upon loading the page if a certain condition is met

As a beginner in this field, I am struggling to assist a friend with a project. He needs a simple quote form that can generate HTML quotes for his client on a private WordPress page. The form should display a specific div based on the radio button selecti ...

Using scripted <svg> with <defs> and attempting to reference it via JavaScript results in failure

My goal is to dynamically generate svg path elements in html using JavaScript. I would like to place these paths within a <defs> element so that they can be reused later in <use> xlink:href elements. However, after creating the paths (by pr ...

Tips for effectively managing loading state within redux toolkit crud operations

Seeking guidance on efficiently managing the loading state in redux-toolkit. Within my slice, I have functionalities to create a post, delete a post, and fetch all posts. It appears that each operation requires handling a loading state. For instance, disp ...

What is the best way to conceal a row when a particular field is devoid of content?

Is it possible to hide an entire table row if a field is empty? For example: If a certain field is empty, I want the entire row to be hidden, either using JavaScript or jQuery. I have tried some methods but none of them fully hide the row. Is there a way ...

Error handling proves futile as Ajax upload continues to fail

Utilizing a frontend jQuery AJAX script, I am able to successfully transfer images onto a PHP backend script hosted by a Slim framework app. However, there is one specific image (attached) that is causing an issue. When the backend attempts to send back a ...

JavaScript - Reveal a div when a grid item is clicked

I have created a grid with a 5x7 layout using divs. I am trying to achieve a feature similar to the Netflix interface where, upon clicking on a div, a larger div will appear beneath it. This larger div should expand to 100% width of the parent container, p ...

Accessing data points in jQuery

Here is a sample code I'm working with : <script type="text/javascript"> var currentPicture;//default picture var picEL;//the image viewer element jQuery("#backImageShower").hover( function(i) { picEL = j ...

Having trouble with Vue i18n and TypeScript: "The '$t' property is not recognized on the 'VueConstructor' type." Any suggestions on how to resolve this issue?

Within my project, some common functions are stored in separate .ts files. Is there a way to incorporate i18n in these cases? // for i18n import Vue from 'vue' declare module 'vue/types/vue' { interface VueConstructor { $t: an ...

Exploring the comparison of information across two sets of databases using Node.js and MongoDB

In the realm of Node.js and MongoDB, I have set up two databases (1-btech2014 & 2-btechre2014) on a single server. My objective is to compare the data in btech2014 with that in btechre2014. If they match, I should retrieve the data from btech2014 as outpu ...

Specify various labels and eliminate collections in behat.yml

I am looking to set up multiple tags in my behat.yml file to represent suite names. Within the features folder, there are 4 suites with several .feature files each. For example: admin, themes The tags I want to define are: @admin, @themes. Previously, ...

Uncovering unseen glitches while coding in Vue.js

After changing the page, I noticed that the errors from the form are still visible. Is there a way to make them disappear when navigating away and only show up if I return to the page? Here are the errors I am encountering: <template> <b-form @ ...

Developing web applications using a combination of PHP, MySQL, and

I am in need of creating an HTML form that functions as CRUD. This form should be able to record inputs such as "row_number", "channel_name", and "ip_address". It will store all the data in a MySQL table using the "ORDER BY row_number" function. The form i ...

The correct method for including a CSS file in a Vue.js application

How can external CSS files be properly added in a Vue.js Application? I have come across multiple methods for adding CSS files in a Vue.js Application. One suggestion is to use the following code: <link rel="stylesheet" type="text/css" href="/static/b ...

What is the best way to assign a unique ID to every element in this situation?

Here is the given code: var words = ['ac', 'bd', 'bf', 'uy', 'ca']; document.getElementById("wordTable").innerHTML = arr2tbl(2); function arr2tbl(ncols) { return words.reduce((a, c, i) => { ...

Tips on preventing the opening of a new browser tab by using Ctrl + click

Hey there, I've got a list of products that can be selected using the Ctrl key. $(parentSelector).on("click", function (evnt) { evnt.stopImmediatePropagation(); var item = $(evnt.delegateTarget) ...