0

There is a drop-down on my screen which shows some selected value and it is disabled. I need the selected value from the drop-down list to put some assertions in Selenium.

How to get the selected value when drop-down is disbaled? enter image description here

I tried using below code:

Select billCycle = new Select(driver.findElement(By.xpath("//select[contains(@id,'BILL_CYC_CD')]")));
String billCycleValue = billCycle.getFirstSelectedOption().getText();

But I am getting below exception:

java.lang.AssertionError: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=110.0.5481.178)
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Priya
  • 1
  • 2

2 Answers2

1

getFirstSelectedOption()

getFirstSelectedOption() returns the first selected option in this select tag (or the currently selected option in a normal select).


This usecase

To print the selected value from the drop-down list you can use either of the following Locator Strategies:

  • Using cssSelector and id attribute:

    WebElement billCycle = new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("select#BILL_CYC_CD")))).getFirstSelectedOption();
    System.out.println(billCycle.getText());
    
  • Using xpath and name attribute:

    WebElement billCycle = new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//[@name='BILL_CYC_CD']")))).getFirstSelectedOption();
    System.out.println(billCycle.getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You are currently trying to get first option from disabled select.

Try instead getting all of the options from dropdown:

List<String> billCycleValues = billCycle.getOptions();
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77