[英]Check that text is visible Selenium + Java
Here is HTML:这是 HTML:
<select size="1" id="F_21__3" class="c3" style="position: absolute; left: 127px; top: 173px; width: 148px; height: 14px; font-style: normal; font-weight: normal; text-decoration: none; text-shadow: none; font-stretch: normal;" disabled="disabled">
<option value="I" des="Item">Item</option>
<option value="S" des="Service">Service</option>
</select>
How to check that text "Service" is visible with Selenium + Java?如何使用 Selenium + Java 检查文本“Service”是否可见?
Code to check if 'Services' is selected:检查是否选择了“服务”的代码:
WebElement selectedByDefault= driver.findElement(By.xpath("//select[@id='F_21__3']"));
Select select = new Select(selectedByDefault);
if(select.getFirstSelectedOption().equals("Service"))
{
System.out.println("Services is displayed by Default.");
}
Code to check if text 'Service' is VISIBLE or not检查文本“服务”是否可见的代码
String checkTextVisible = driver.findElement(By.xpath("//*[contains(text(),'Service')]").getText();
if(checkTextVisible.equals("Service")){
System.out.println(checkTextVisible + " text is visible");
}
It's not important if the field is disabled or enabled.该字段是禁用还是启用并不重要。 You just find the correct selector and after that you can check it very easy.
您只需找到正确的选择器,然后就可以非常轻松地进行检查。
String actualText = driver.findElemeny(By.cssSelector("yourSelector")).getAttribut("value");
Assert.assertEqual(actualText, "Service");
or more simple (but only if Service don't appear in another place on your page).或更简单(但前提是 Service 没有出现在您页面上的其他位置)。
boolean presence = false;
if(driver.getPageSource().contains("Service")){
System.out.println("Text is present");
presence = true;
}else{
System.out.println("Text is absent");}
Assert.assertTrue(presence);
You can use getFirstSelectedOption()
to get the visible selected dropdown.您可以使用
getFirstSelectedOption()
来获取可见的选定下拉列表。
@Findby(id="F_21__3")
private WebElement selectID;
Select s = new Select(selectID);
String selected = s.getFirstSelectedOption().getText();
Assert.assertTrue(selected.equals("Service"));
如果您想检查是否选择了“服务”,下面的代码将返回真/假:
driver.findElement(By.xpath("//select[@id='F_21__3']/option[.='Service']")).isSelected()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.