Java利用Selenium操作瀏覽器的示例詳解
簡介
本文主要介紹如何使用java代碼利用Selenium操作瀏覽器,某些網頁元素加載慢,如何操作元素就會把找不到元素的異常,此時需要設置元素等待,等待元素加載完,再操作。
設置元素等待
很多頁面都使用 ajax 技術,頁面的元素不是同時被加載出來的,為了防止定位這些尚在加載的元素報錯,可以設置元素等來增加腳本的穩(wěn)定性。webdriver 中的等待分為 顯式等待 和 隱式等待。
顯式等待
顯式等待:設置一個超時時間,每個一段時間就去檢測一次該元素是否存在,如果存在則執(zhí)行后續(xù)內容,如果超過最大時間(超時時間)則拋出超時異常(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秒內元素加載出來就執(zhí)行點擊
shortWait.until(ExpectedConditions.elementToBeClickable(element)).click();
//忽略找不到元素異常
shortWait.ignoring(NoSuchElementException.class);
System.out.println("耗時 "+(System.currentTimeMillis()-start)+" ms");
}
}- until 指定預期條件的判斷方法,在等待期間,每隔一段時間調用該方法,判斷元素是否存在,直到元素出現。
- ignoring 指定忽略的異常,如果設定的執(zhí)行等待超時的時間段內,忽略指定的異常,讓程序繼續(xù)進行。
隱式等待
隱式等待也是指定一個超時時間,如果超出這個時間指定元素還沒有被加載出來,就會拋出 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"));這句之后觸發(fā)隱式等待,在輪詢檢查 5s 后仍然沒有定位到元素,拋出異常。

強制等待
用java代碼強制當前正在執(zhí)行的線程休眠(暫停執(zhí)行)
使用 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");
}
}
}
值得一提的是,對于定位不到元素的時候,從耗時方面隱式等待和強制等待沒什么區(qū)別。但如果元素經過 2s 后被加載出來,這時隱式等待就會繼續(xù)執(zhí)行下面的代碼,但 sleep還要繼續(xù)等待 3s。
總結
推薦使用的隱式等待,也就是implicitlyWait。
理由:
使用implicitlyWait或者明確等待(explicitly wait),方法參數是等待的最大時長。
也就是只要一找到元素,就會立刻執(zhí)行下一行代碼,不會強制等待參數里設置的時間。
而第三種(線程休眠)則不同,會強制等待設置的時間。設想一下,如果你的工程有好幾百個case,
需要等待的元素都采用第三種,會大大加長所有case執(zhí)行的時間,而你又急著要report,豈不是很慘。
使用implicitlyWait,webdriver會自動應用到case中的所有element中。在啟動瀏覽器(driver.get)之后設置上,這樣就不用針對某個元素去設置了,簡直太方便了,不過有些特殊的元素,確實等待時間較長的,可以再采用explicit wait。
如果implicitlyWait和explicitly wait都在用在代碼里,那么最大等待時間不是兩個時間的疊加,而是取最大值。
到此這篇關于Java利用Selenium操作瀏覽器的示例詳解的文章就介紹到這了,更多相關Java Selenium操作瀏覽器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中Collection集合常用API之?Collection存儲自定義類型對象的示例代碼
Collection是單列集合的祖宗接口,因此它的功能是全部單列集合都可以繼承使用的,這篇文章主要介紹了Java中Collection集合常用API?-?Collection存儲自定義類型對象,需要的朋友可以參考下2022-12-12
java中的日期時間類Date和SimpleDateFormat
這篇文章主要介紹了java中的日期時間類Date和SimpleDateFormat,Date類的對象在Java中代表的是當前所在系統的此刻日期時間,說白了就是你計算機上現實的時間,需要的朋友可以參考下2023-09-09
Mybatis之Select Count(*)的獲取返回int的值操作
這篇文章主要介紹了Mybatis之Select Count(*)的獲取返回int的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
String字符串轉BigDecimal時,報NumberFormatException異常的解決
這篇文章主要介紹了String字符串轉BigDecimal時,報NumberFormatException異常的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
TransactionSynchronization的invokeAfterCompletion事務源碼解析
這篇文章主要為大家介紹了TransactionSynchronization的invokeAfterCompletion事務源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

