1

I am working on selenium webdriver, and I came up with a certain scenario. There is a drop down list, and user selects one of the values (Text). The selected value (text) is now displayed on the drop down field.

I want to know how can I extract the text out of that element (div) and then assert it with the selected value (text).

The HTML of the element is:

<div class="kac-entry kac-select kac-display ng-binding" ng-click="toggleDropdown()" ng-hide="shouldShowPlaceholder()" ng-class="{btn: btnStyle, 'btn-primary': btnStyle}" style="">      ^NYA (NYSE COMPOSITE INDEX (NEW METHOD))   </div>

The xpath of the above element is:

//price-trigger[@class='ng-isolate-scope']/div/div[2]/div[1]/div[1]/k-select/div/div[1]

The text in the HTML element is ^NYA (NYSE COMPOSITE INDEX (NEW METHOD)). I want to get this title and check if this is the option (value) that user actually selected or not.

Any help will be highly appreciated.

1 Answer 1

1

Just wrap your WebElement into Select Object as shown below

Select select = new Select(driver.findElement(By.id("groupSelect")));

if you want to select by xpath

By.xpath("//price-trigger[@class='ng-isolate-scope']/div/div[2]/div[1]/div[1]/k-select/div/div[1]")

Assert.assertEquals("Please select any option...", select);

Syntax:

String selectedOption = new Select(driver.findElement(By.xpath("Type the xpath of the drop-down element"))).getFirstSelectedOption().getText();

Assert.assertEquals("Please select any option...", selectedOption);

More in detailed on selecting value from dropdown

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

Now to identify dropdown do

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do

dropdown.selectByVisibleText("Programmer ");

or

dropdown.selectByIndex(1);

or

dropdown.selectByValue("prog");
Sign up to request clarification or add additional context in comments.

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.