如何验证一个元素是否在 Selenium 中显示在屏幕上?
我们可以借助以下列出的方法帮助验证 Web 元素(如编辑框、复选框和单选按钮等)是否可见 −
isDisplayed()
此方法检查 Web 元素是否显示在屏幕上。
语法 −
Boolean result = driver.findElement(By.xpath("//span[text()=’Coding Ground’]")).isDispayed();isSelected()
此方法检查静态下拉菜单中单选按钮、复选框和选项的状态。
语法 −
Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,’gsc-search-button’)]")).isSelected();isEnabled()
语法 −
Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,’gsc-search-button’)]")).isEnabled();
此方法检查一个元素是否已启用。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ElementStatus{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url =
"https://tutorialspoint.com/tutor_connect/index.php";
// to verify if a static dropdown is selected with option isSelected()
boolean drpdwnStatus = driver.findElement(By.xpath("//select[@name=’selType’]"))
.isSelected();
// to verify if an element is present on page with isDisplayed()
boolean editStatus = driver.findElement(By.xpath("//input[@id=’txtSearchText’]"))
.isDisplayed();
// to verify if a button is enabled with isEnabled()
boolean butnStatus = driver.findElement(By.xpath("//input[@id=’searchSubmit’]"))
.isEnabled();
System.out.println("The button status is " + butnStatus);
System.out.println
("The dropdown selected status is" + drpdwnStatus);
System.out.println("The edit box display status is " + editStatus);
driver.close();
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP