Obtaining the displayed value from a disabled dropdown list with Selenium Webdriver in Java

Is there a method to extract the value displayed on a disabled dropdown list? I attempted the code below:

Select select = new Select(Locator);
WebElement option = select.getFirstSelectedOption();
String text= option.getText();

Issue Encountered: The error message states that the element should have been of type 'select' but was identified as an 'input'. However, the element is indeed a 'Select' element that is just disabled.

HTML Structure:

<select class="form-control dirty-checked-field" disabled="disabled" id="Pyear" name="Sections[0].PortfolioYear" title="2019">

Answer №1

public void retrieveValueByID(String id, int index) {
    try {
        WebElement element = driver.findElement(By.id(id));
        Select select = new Select(element);
        List<WebElement> options = select.getOptions();
        options.get(index).getText();
    } catch (Exception e) {
        // Handle errors
    }
}

Where:

id is *Pyear*
index represents the position of the drop-down list item. 

For example, if the drop-down list has only one element, the index should be 0.

Other suggestions:

  1. Attempt to retrieve the value using xpath.
  2. Attempt to use attribute= disabled to access the value.

Thank you,

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

Pass intricate JavaScript object to ASP.Net MVC function

It appears that many people have shared helpful answers on a common topic, but I am still facing difficulties in making my attempt work. The issue is similar to the one discussed here, however, I am only trying to send a single complex object instead of a ...

It is recommended to use `new EdgeOptions()` over `DesiredCapabilities.edge()` when encountering issues launching the Remote Microsoft Edge browser via Jenkins and Selenium

When it comes to testing a specific website on the remote edge browser using Selenium in Java, I'm facing some challenges. The code for the remote edge browser seems to be missing, although testing locally on the edge browser presents no issues. The ...

Utilize Angular components in TypeScript to effectively manage errors within forms

I am currently developing a form in Angular/Typescript with more than 10 fields, and I want to streamline error management without redundancy in my HTML code. Here is an example of the form structure : <form [formGroup]="myForm"> <label&g ...

React Button changes style when clicked

I have a scenario where I am passing an array of objects from an external JavaScript file to the main component for rendering: const students = filteredStudents.map(i => <div key={i.id} className='d-flex border-bottom'> <img src ...

Issues with Pdf.js functionality on Internet Explorer

Could someone shed some light on why this code isn't functioning in Internet Explorer? It works perfectly fine in Chrome. Snippet of my HTML: <head> <script type='text/javascript' src="//code.jquery.com/jquery-1.9.1.js"></sc ...

Pass Form ID To Function In A Dynamic Way

I have multiple forms on my webpage and I want to use the same ajax function for all of them. It currently works well for one form by fetching the id with getElementById and then passing it to the ajax function. My goal is to dynamically pass down the form ...

Angular can be used to determine the height of table rows

Recently, I've been dealing with a table structure that dynamically adjusts its row count based on the size of its container and the individual height of each row. The interesting part comes with the link column - each row has the capability of conta ...

The class org.openqa.selenium.WebDriver cannot be found and is causing a java.lang.NoClassDefFoundError

Here is the code that I have written, it's quite basic. However, when I try to run it, I encounter the following error: Error: Unable to initialize main class testPackage.myTestClass Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/Web ...

Discovering the largest value with arrays in javascript

My code is functioning as expected for single-digit numbers but encounters issues with two or three-digit numbers. I want to stick to this approach without utilizing the math max function. The primary issue lies in only considering the first digit of a tw ...

Am I using the correct method to parseInt within an if-else statement for Datatable?

Just started working with JS BS Datatables, wondering if this is the correct approach for using parseInt in an if else statement? $(document).ready(function() { $('#myTable').DataTable({ "order": [], "columnDefs ...

JavaScript's ability to utilize local files

Has anyone successfully performed local file manipulation using JavaScript without any installation requirement like Adobe AIR? I am specifically interested in reading the contents of a file and writing them to another file. I am not concerned about permi ...

Turning spring form data into a JSON object via automation (with a mix of Spring, jQuery, AJAX, and JSON)

Recently, I've set up a spring form that utilizes ajax for submission. Here's an overview of my form... <form:form action="addToCart" method="POST" modelAttribute="cartProduct"> <form:input type="hidden" ...

Creating a new JSON object from an array of objects

I have a variety of dropdown menus on my website. Depending on the choices made in these dropdowns, I want to create a JSON object that follows this structure: {"list":{"city":["1","2","3"],"businessName":["City1","AnotherCity"]}} Here is the code for ha ...

Position the Material-UI AppBar and Tab on the far right of the screen

I am trying to achieve a specific layout where the Links for "Homepage Login Settings and etc.." are placed at the right edge of the AppBar, while keeping the "Website" aligned to the left edge of the screen. Here is what I have: https://i.sstatic.net/Wvo ...

Unraveling the Mystery of React Event Bubbling: Locating the Desired

I am working with a <ul> Component that wraps several <li> Components. To streamline my code, I would like to avoid adding an individual onClick handler to each li and instead utilize a single handler on the parent ul element to capture the bub ...

Running a function when an Angular PrimeNg Checkbox is checked

Recently, I’ve been working on implementing a functionality that triggers when a checkbox within a datatable is clicked. The scenario involves a default set of values displayed in the table (ranging from 1 to 10). Additionally, I provide an array of sele ...

Is Firefox recording session storage information along with browser history?

When using Windows 10, I encountered a scenario where there is a server that accepts a POST to /random. Upon receiving the request, the server generates an HTML file with a new random number embedded in JavaScript. This JavaScript is responsible for displa ...

Include a stylesheet as a prop when rendering a functional component

Using Next.js, React, Styled JSX, and Postcss, I encountered an issue while trying to style an imported component for a specific page. Since the stylesheets are generated for a specific component at render time, I attempted to include custom styles for the ...

What is the best way to use selenium for iterating through multiple pages on a website?

Recently, I've been attempting to create a Python script to scrape a specific website for a list of all URLs. The website in question is . The main challenge I've encountered is that the website utilizes Javascript for navigation, making it diff ...

We are sorry, but the requested route cannot be located

I'm diving into the world of express and trying to set up subroutes for a specific "plan" with corresponding actions. My journey begins at mypage.com/someroute/123321312 router.get('/:planId', function(req, res, next) { //a form is render ...