首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检查文本是否可见Selenium + Java

检查文本是否可见Selenium + Java
EN

Stack Overflow用户
提问于 2018-08-06 20:20:04
回答 5查看 1.4K关注 0票数 -1

下面是HTML:

代码语言:javascript
复制
<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>

如何使用Selenium + Java检查文本"Service“是否可见?

EN

回答 5

Stack Overflow用户

发布于 2018-08-06 20:39:57

检查是否选择了“Services”的代码:

代码语言:javascript
复制
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.");
}
票数 3
EN

Stack Overflow用户

发布于 2018-09-12 18:42:58

检查文本“Service”是否可见的代码

代码语言:javascript
复制
 String checkTextVisible = driver.findElement(By.xpath("//*[contains(text(),'Service')]").getText();
if(checkTextVisible.equals("Service")){
   System.out.println(checkTextVisible + " text is visible");
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-01 22:01:07

该字段是禁用还是启用并不重要。你只需要找到正确的选择器,然后你就可以很容易地检查它了。

代码语言:javascript
复制
String actualText = driver.findElemeny(By.cssSelector("yourSelector")).getAttribut("value");
Assert.assertEqual(actualText, "Service");

或者更简单(但前提是Service不会出现在页面的其他位置)。

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51707504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档