Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dropdown option value using dropdown option in selenium webdriver

I am developing framework for selenium automation and I have got stuck to get the dropdown option's value using dropdown. I have dropdown option but now I want the value of same dropdown. Is it possible to get it in selenium.

For example:

Here is the Html tag:

    <select id="periodId" name="period" style="display: none;">
    <option value="week1">1</option>
    <option value="week2">2</option>
    <option value="week3">3</option>
    <option value="week4">4</option>
    <option value="week5">5</option>
    <option value="week16">6</option>
    </select>

Now in the framework which I have developed, I will pass the dropdown option '5', now I want the value of dropdown option '5'. Is that possible to get value 'week5' for dropdown option '5' using selenium?

like image 906
Karim Narsindani Avatar asked Oct 23 '25 04:10

Karim Narsindani


1 Answers

Try this and let me know.

Select period = new Select(driver.findElement(By.id("periodId")));

period.selectByVisibleText("5");

String value = period.getFirstSelectedOption().getAttribute("value");
like image 110
Purus Avatar answered Oct 26 '25 10:10

Purus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!