zoukankan      html  css  js  c++  java
  • 元素同步问题解决----自定义类

    在自动化测试脚本开发过程中,很大部分的报错是由于元素因为时间不同步而产生的。本文总结了用自定义的类库来解决元素同步问题。

    首先,新建一个自定义的类方法,对它进行编码。

    package first;

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    public class MyWait {

     

        public  static WebElement isElementPresent(WebDriver driver,String xpath,int time)

        {

           WebElement element=null;

          

           for(int i=0;i<time;i++)

           {

               try {

                  element=driver.findElement(By.xpath(xpath));

                  break;

               } catch (Exception e) {

                    try {

                      Thread.sleep(1000);

                  } catch (InterruptedException e2) {

                      System.out.println("Waiting for the element to appear");

                  }

     

               }

           }

           return element;

        }

     

    }

    在另外一个包下新建一个测试类,测试调用类方法。

    package Test;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.firefox.FirefoxDriver;

    import first.MyWait;

    public class MyTest {

    public static void main(String[] args) {

        WebDriver driver=new FirefoxDriver();

        driver.manage().window().maximize();

        driver.get("https://www.apache.org/");

        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

        System.out.println(MyWait.isElementPresent(driver, ".//*/section[@class='bg-gray']/div/div/div[3]/h3", 10).getText());

    }

    }

    根据元素xpath去定位元素,在10秒之内不断循环去定义该元素,如果在页面出现,就可以定位成功,否则抛出异常。

  • 相关阅读:
    计算机视觉(四)
    计算机视觉(三)
    计算机视觉(二)
    计算机视觉(一)
    基于opencv3实现运动物体识别
    tensorRT程序设计框架_4
    Cuda程序的设计-2
    神经网络的快速体验
    STL简介_18
    函数模板-17
  • 原文地址:https://www.cnblogs.com/miaojjblog/p/9685149.html
Copyright © 2011-2022 走看看