4

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?

1 Answer 1

8

Try this and let me know.

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

period.selectByVisibleText("5");

String value = period.getFirstSelectedOption().getAttribute("value");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your inputs, it is working as expected with your sample code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.