1

just want to print the value of selected option after selecting the option from dropdown .

package Webbasics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class ecommerce {
    public static void main(String args[]) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C:\\Program Files\\selenium\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
            driver.get("http://live.demoguru99.com/index.php/");
            driver.manage().window().maximize();
            driver.findElement(By.xpath("//*[@id=\"nav\"]//li[1]/a")).click();
            
            Select sortBy=new Select(driver.findElement(By.xpath("(//select[contains(@title,\"Sort By\")])[1]")));
            
            sortBy.selectByIndex(1);
            Select sortBy1=new Select(driver.findElement(By.xpath("(//select[contains(@title,\"Sort By\")])[1]")));
            WebElement selected=sortBy1.getFirstSelectedOption();
            
            System.out.println(selected.getText());
            
        }
}

I am getting proper result but i think its not the best way of writing select class two times so can you help me to write in a better way

Bhargav
  • 21
  • 4
  • Remove the declaration of `sortBy1` and use `WebElement selected=sortBy.getFirstSelectedOption();` Unless the setting of the index causes the element `sortBy` to become `stale`, in which case, your code is correct. – Ryan Wilson Sep 28 '20 at 13:58
  • yes, it thrown stale element exception that's why I declared it with another object reference – Bhargav Sep 30 '20 at 04:17
  • Then your code is correct. You can't use the same object since it goes stale. You could however use the same variable, but set it's value again instead of using `sortBy1` -> `sortBy=new Select(driver.findElement(By.xpath("(//select[contains(@title,\"Sort By\")])[1]")));` – Ryan Wilson Sep 30 '20 at 12:11

2 Answers2

0

You have correct way with find element again after select the dropdown element, but after selected you should wait a while to make the element visible again.

See the following code:

Select sortBy = new Select(driver.findElement(By.xpath("(//select[contains(@title,'Sort By')])[1]")));
sortBy.selectByIndex(1);

//wait here
WebDriverWait wait = new WebDriverWait(driver, 20);
sortBy = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//select[contains(@title,'Sort By')])[1]"))));
System.out.println(sortBy.getFirstSelectedOption().getText());

But above I find again the element without creating a new variable name for the dropdown, still sortBy. Although with initialization the new variables should work too.

Don't forget to import the following:

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

Reference discussion

frianH
  • 7,295
  • 6
  • 20
  • 45
0

Firstly, you have to wait element visible. Secondly, use this code

Select select = new Select("Element");
WebElement tmp = select.getFirstSelectedOption();