title: How to print selected option from drowdown by using selenium java?
body: <p>just want to print the value of selected option after selecting the option from dropdown .</p>
  <pre><code>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(&quot;webdriver.chrome.driver&quot;,&quot;C:\\Program Files\\selenium\\chromedriver.exe&quot;);
          WebDriver driver=new ChromeDriver();
              driver.get(&quot;http://live.demoguru99.com/index.php/&quot;);
              driver.manage().window().maximize();
              driver.findElement(By.xpath(&quot;//*[@id=\&quot;nav\&quot;]//li[1]/a&quot;)).click();
              
              Select sortBy=new Select(driver.findElement(By.xpath(&quot;(//select[contains(@title,\&quot;Sort By\&quot;)])[1]&quot;)));
              
              sortBy.selectByIndex(1);
              Select sortBy1=new Select(driver.findElement(By.xpath(&quot;(//select[contains(@title,\&quot;Sort By\&quot;)])[1]&quot;)));
              WebElement selected=sortBy1.getFirstSelectedOption();
              
              System.out.println(selected.getText());
              
          }
  }
  </code></pre>
  <p>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</p>