如何使用Selenium/Java验证页面中元素的存在

如何使用Selenium/Java验证页面中元素的存在,java,selenium,verify,Java,Selenium,Verify,我试图找出一种方法来查看页面上是否存在元素 这就是我目前所拥有的 但是,如果元素不存在,将抛出异常,脚本将停止 有人能帮我找到更好的方法吗 //Checking navbar links System.out.println("======================="); System.out.println("Navbar link checks"); //Checking for Web link element in Nav bar

我试图找出一种方法来查看页面上是否存在元素

这就是我目前所拥有的

但是,如果元素不存在,将抛出异常,脚本将停止

有人能帮我找到更好的方法吗

//Checking navbar links
        System.out.println("=======================");
        System.out.println("Navbar link checks");
    //Checking for Web link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a"))!= null){
            System.out.println("Web link in Navbar is Present");
            }else{
            System.out.println("Web link in Navbar is Absent");
            }
    //Checking for Images link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[2]/a"))!= null){
            System.out.println("Images link in Navbar is Present");
            }else{
            System.out.println("Images link in Navbar is Absent");
            }
    //Checking for News link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[3]/a"))!= null){
            System.out.println("News link in Navbar is Present");
            }else{
            System.out.println("News link in Navbar is Absent");
            }
    //Checking for Videos link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[4]/a"))!= null){
            System.out.println("Videos link in Navbar is Present");
            }else{
            System.out.println("News link in Navbar is Absent");
            }
    //Checking for Maps link element in Nav bar
           if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[5]/a"))!= null){
            System.out.println("Maps link in Navbar is Present");
            }else{
            System.out.println("Maps link in Navbar is Absent");
            }

您可以使用两种不同的方法。为此,我可能建议使用
findElements

if(driver.findElements(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a"))!= 0)

为了避免出现例外情况,您只需编写自己的助手来处理并返回
null

public class Helper {
    private static final WebDriver driver;
    public static WebElement findElement(By locator) {
        try {
            return driver.findElement(locator);
        } catch (NoSuchElementException notFound) {
            return null;
        }
    }

    // initialize Helper with your driver on startup
    public static init(WebDriver yourDriver) {
        driver = yourDriver;
    }
}
然后在代码中调用它:

//Checking for Web link element in Nav barl
WebElement element = Helper.findElement(By.xpath("my/complex/xpath"));
if(element == null) {
    System.out.println("My Element is Absent");
}

抱歉,这是一个非常愚蠢的问题,但是helper类将放在哪里呢?在“main”函数之外,在同一个java文件中?您可以创建standalone Helper.java并将该类放入其中,然后您可以访问project.Hmm中的所有Helper方法。我认为我的整个思考过程是错误的。在我尝试自动化测试的网页中,有一个id为“topnav”的导航栏。根据我正在查看的页面版本,导航栏将具有不同的导航链接。我如何能够遍历导航栏,输出其中的所有导航链接?您可以从获取WebElement navBar=Helper.findelelement(by.id(“topnav”))开始;然后在导航栏中搜索元素:
navBar.findElement
。您必须自己实现逻辑。你可以就此单独问一个问题。我只是试图解决您最初提出的由于异常而终止java代码的问题,我理解kiruwka。谢谢你的帮助。我现在就去想办法。