这是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>
如何使用Selenium + Java检查文本“Service”是否可见?
用于检查是否选择“服务”的代码:
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.");
}
用于检查文本“服务”是否可见的代码
String checkTextVisible = driver.findElement(By.xpath("//*[contains(text(),'Service')]").getText();
if(checkTextVisible.equals("Service")){
System.out.println(checkTextVisible + " text is visible");
}
如果您想检查是否选择了“服务”,下面的代码将返回true / false:
driver.findElement(By.xpath("//select[@id='F_21__3']/option[.='Service']")).isSelected()