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

后端 未结 2 1802
抹茶落季
抹茶落季 2021-01-29 03:33

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 pa

相关标签:
2条回答
  • 2021-01-29 04:23

    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
      ===============================================
      
    0 讨论(0)
  • 2021-01-29 04:25

    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());
    
    0 讨论(0)
提交回复
热议问题