简体   繁体   中英

How to print the text of the selected option choosen through 'selectByVisibleText' method in selenium

I am new to Selenium, could any one of you let me know how to print the value selected from 'selectByVisibleText' in selenium?

I have run the test on Facebook login page, drop down for Birthday.

Thanks in Advance.

Once you have selected the option by visible text, you can get the option as a WebElement and getText() from it.

Example:

Select select = new Select(driver.findElement(By.id("some-id")));
select.selectByVisibleText("some-text");
WebElement element = select.getFirstSelectedOption();
System.out.println(element.getText());

Once you select the option through selectByVisibleText() method to print the value you need to invoke getFirstSelectedOption() method as follows:

  • Code Block:

     Select month_dd = new Select(month_dropdown); month_dd.selectByVisibleText("Dec"); WebElement myElem = month_dd.getFirstSelectedOption(); System.out.println(myElem.getText()); 
  • Console Output:

     Dec PASSED: selectDDvalues =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM