1

I'm developing a automation that needs to access the first (and only one) dropdown value. I want to get this "Unavailable date" value to do a validation in the future. This is the HTML:

<div class = "content-box-wrapper">
    <fieldset>
        <li>
            <label>Select a time</label>
    
            <div>
                <select name = "idAgenda" id="hourSelected">
                    <option value>Unavailable date</option>
                </select>
            </div>
        </li>
    </fieldset>    
</div> 

3 Answers 3

1
import org.openqa.selenium.support.ui.Select;
Select hours = new Select(driver.findElement(By.id("hoursSelected")));


hours.selectByIndex(1);
hours.selectByVisibleText("Unavailable date");
hours.selectByValue("Unavailable date")

Since it's in a Select tag you can use the following import and select by index or value.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the below xPath to get the selected value of drop-down.

//select[@id='hourSelected']/option

In case, if drop-down is having multiple values then if it have selected tag for selected value. Please write xPath as below,

HTML

<option value="">Unavailable date1</option>
<option selected="" value="">Unavailable date2</option>
<option value="">Unavailable date3</option>

xPath

//select[@id='hourSelected']/option[@selected]

Comments

0

I got it using getText() method:

WebElement disponibilidade = chrome.findElement(By.xpath("//*[@id=\"hourSelected\"]"));
String text1 = disponibilidade.getText();

Comments

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.