如何使用JAVA基于其文本Selenium WebDriver断言元素
我正在开发SeleniumWebDriver,我提出了一个特定的场景。 有一个下拉列表,用户选择其中一个值(文本)。所选值(文本)现在显示在下拉字段中 我想知道如何从该元素(div)中提取文本,然后用所选值(text)断言它 元素的HTML为:如何使用JAVA基于其文本Selenium WebDriver断言元素,java,html,selenium-webdriver,Java,Html,Selenium Webdriver,我正在开发SeleniumWebDriver,我提出了一个特定的场景。 有一个下拉列表,用户选择其中一个值(文本)。所选值(文本)现在显示在下拉字段中 我想知道如何从该元素(div)中提取文本,然后用所选值(text)断言它 元素的HTML为: <div class="kac-entry kac-select kac-display ng-binding" ng-click="toggleDropdown()" ng-hide="shouldShowPlaceholder()" ng-cl
<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>
HTML元素中的文本是^NYA(NYSE复合索引(新方法))。我想得到这个标题,并检查这是否是用户实际选择的选项(值)
非常感谢您的帮助。只需将您的WebElement包装到选择对象中,如下所示
Select select = new Select(driver.findElement(By.id("groupSelect")));
如果要通过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);
语法:
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);
有关从下拉列表中选择值的详细信息
<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>
要选择它的选项,可以说“程序员”
dropdown.selectByVisibleText("Programmer ");
或
或
Select dropdown = new Select(driver.findElement(By.id("designation")));
dropdown.selectByVisibleText("Programmer ");
dropdown.selectByIndex(1);
dropdown.selectByValue("prog");