软件编程
位置:首页>> 软件编程>> java编程>> Java利用Selenium操作浏览器的示例详解

Java利用Selenium操作浏览器的示例详解

作者:洛阳泰山  发布时间:2022-06-17 17:34:20 

标签:Java,Selenium,浏览器

简介

本文主要介绍如何使用java代码利用Selenium操作浏览器,某些网页元素加载慢,如何操作元素就会把找不到元素的异常,此时需要设置元素等待,等待元素加载完,再操作。

设置元素等待

很多页面都使用 ajax 技术,页面的元素不是同时被加载出来的,为了防止定位这些尚在加载的元素报错,可以设置元素等来增加脚本的稳定性。webdriver 中的等待分为 显式等待 和 隐式等待。

显式等待

显式等待:设置一个超时时间,每个一段时间就去检测一次该元素是否存在,如果存在则执行后续内容,如果超过最大时间(超时时间)则抛出超时异常(TimeoutException)。显示等待需要使用 WebDriverWait,同时配合 until 或 not until 。下面详细讲解一下。

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
* @author Lenovo
*/
public class SeleniumDemo {
   private final static String webDriver = "webdriver.chrome.driver";
   private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

public static void main(String[] args) throws InterruptedException {
       System.setProperty(webDriver, webDriverPath);
       WebDriver driver= new ChromeDriver();
       //博客主页
       driver.get("https://blog.csdn.net/weixin_40986713");
       WebElement element=driver.findElement(By.className("submit"));
       long start=System.currentTimeMillis();
       //等待5秒
       WebDriverWait shortWait = new WebDriverWait(driver, 5);
       //5秒内元素加载出来就执行点击
       shortWait.until(ExpectedConditions.elementToBeClickable(element)).click();
       //忽略找不到元素异常
       shortWait.ignoring(NoSuchElementException.class);
       System.out.println("耗时 "+(System.currentTimeMillis()-start)+" ms");
   }
}
  • until 指定预期条件的判断方法,在等待期间,每隔一段时间调用该方法,判断元素是否存在,直到元素出现。

  • ignoring 指定忽略的异常,如果设定的执行等待超时的时间段内,忽略指定的异常,让程序继续进行。

隐式等待

隐式等待也是指定一个超时时间,如果超出这个时间指定元素还没有被加载出来,就会抛出 NoSuchElementException 异常。

除了抛出的异常不同外,还有一点,隐式等待是全局性的,即运行过程中,如果元素可以定位到,它不会影响代码运行,但如果定位不到,则它会以轮询的方式不断地访问元素直到元素被找到,若超过指定时间,则抛出异常。

使用 driver.manage().timeouts().implicitlyWait() 来实现隐式等待,使用难度相对于显式等待要简单很多。

示例:打开个人主页,设置一个隐式等待时间 5s,通过 id 定位一个不存在的元素,最后打印 抛出的异常 与 运行时间。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
* @author Lenovo
*/
public class SeleniumDemo {
   private final static String webDriver = "webdriver.chrome.driver";
   private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

public static void main(String[] args) throws InterruptedException {
       System.setProperty(webDriver, webDriverPath);
       WebDriver driver= new ChromeDriver();
       //博客主页
       driver.get("https://blog.csdn.net/weixin_40986713");
       //设置全局隐式等待
       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
       long start=System.currentTimeMillis();
       try {
           driver.findElement(By.className("tarzan"));
       }catch (Exception e){
           System.out.println(e);
           System.out.println("耗时 "+(System.currentTimeMillis()-start)+" ms");
       }
   }
}

代码运行到 driver.findElement(By.className("tarzan"));这句之后触发隐式等待,在轮询检查 5s 后仍然没有定位到元素,抛出异常。

Java利用Selenium操作浏览器的示例详解

强制等待

用java代码强制当前正在执行的线程休眠(暂停执行)

使用 time.sleep() 强制等待,设置固定的休眠时间,对于代码的运行效率会有影响。

以上面的例子作为参照,将 隐式等待 改为 强制等待。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
* @author Lenovo
*/
public class SeleniumDemo {
   private final static String webDriver = "webdriver.chrome.driver";
   private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

public static void main(String[] args) throws InterruptedException {
       System.setProperty(webDriver, webDriverPath);
       WebDriver driver= new ChromeDriver();
       //博客主页
       driver.get("https://blog.csdn.net/weixin_40986713");
       long start=System.currentTimeMillis();
       //等待5s
       Thread.sleep(5000);
       try {
           driver.findElement(By.className("tarzan"));
       }catch (Exception e){
           System.out.println(e);
           System.out.println("耗时 "+(System.currentTimeMillis()-start)+" ms");
       }
   }
}

Java利用Selenium操作浏览器的示例详解

值得一提的是,对于定位不到元素的时候,从耗时方面隐式等待和强制等待没什么区别。但如果元素经过 2s 后被加载出来,这时隐式等待就会继续执行下面的代码,但 sleep还要继续等待 3s。

来源:https://blog.csdn.net/weixin_40986713/article/details/128576798

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com