[JAVA 6#] [Web Automation 6#] Controlling STEEMIT web pages with Selenium #4 WebDriverWait [CN]

in #cn5 years ago

redsjavahouse1591357_1280.jpg
image source: pixabay


今天学习webdriver的等待时间。网页的载入和网速有很大的关系,网速慢的时候网页元素载入的速度理所当然的慢。这时给网速加动作肯定会抛出错误。
上几期我们用了java的sleep函数来强制等待了几秒,但是如果用太多强制等待,代码非常乱,维护起来也很麻烦。
可能是因为这样的原因,selenium给出了几个自己的等待模式,叫implicitly Wait, Explicitly Wait, Fluent Wait。

1. implicitly Wait

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

这个方法可用在整个Webdriver的生命周期。它会等待页面全部元素家在完成而不是单一元素。在三个方法中写法最简单,在代码的开头写一行即可。
于sleep不同之处是在设置的时间内成功找到元素即可执行下一句代码。它包含在manage()函数的timeOuts()函数内, 参数分别为等待时间和时间单位。
image.png

缺点 - 因等待页面全部元素成功载入,所以在有些场景效率不是很高。如要找的元素已经载入但是js没载入完的时候它还是会等待。

2. Explicitly Wait

Wait<WebDriver> wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("로그인dddd")));

还有一个叫 WebDriverWait, 它可以任选元素,自由度非常高。参数为WebDriver和秒单位的数。
因必须和 until()函数一起使用,代码的长度会增多。
until() 函数可以用 Selenium 提供的 ExpectedConditions 类的各种条件来输入参数。ExpectedConditions 详细页面

还可以直接调用 WebDriverWait的父级类 FluentWait 类。FluentWait的自由度比webdriverwai还高一点,附加函数如下。
ignoring() - 可以设置特定 Exception
withTimeout() - 可以设置时间
pollingEvery() - 可以设置每调用的时间

(FluentWait可以直接创建对象。 )


上述内容用代码整理如下

package com.steem.webatuo;

import java.time.Duration;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Steemit {

    public static void main(String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://steemit.com/@june0620/feed");
        driver.manage().window().maximize();

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

        Wait<WebDriver> wait1 = new WebDriverWait(driver, 2);
        wait1.until(ExpectedConditions.presenceOfElementLocated(By.linkText("로그인dddd")));
        
        
        Wait<WebDriver> wait2 = new WebDriverWait(driver, 2).
                ignoring(NoSuchElementException.class).
                withMessage("抛出了~~~~~~错误!!!").
                pollingEvery(Duration.ofMillis(700));
        wait2.until(ExpectedConditions.presenceOfElementLocated(By.linkText("로그인dddd")));
        

        Wait<WebDriver> wait3 = new FluentWait<WebDriver>(driver).
                ignoring(NoSuchElementException.class).
                withTimeout(Duration.ofSeconds(20)).
                withMessage("抛出了~~~~~~错误!!!").
                pollingEvery(Duration.ofMillis(700));
        wait3.until(ExpectedConditions.presenceOfElementLocated(By.linkText("로그인dddd")));

}

.
.
.
.
[Cookie 😅]
Seleniun java lib version: 3.141.59

Sort:  

你今天过的开心吗?想来玩目前STEEM上最火爆的drugwars游戏吗?还在等待什么?赶快加入战斗吧!drugwars.io假如我的留言打扰到你,请回复“取消”。

!thumbup
恭喜你!您的这篇文章入选 @justyy 今日 (2020-02-02) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com

Congratulations! This post has been selected by @justyy as today's (2020-02-02) 【Good Posts】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^

SteemIt 工具、API接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial



If you believe what I am doing, please consider a spare vote voting me here, thank you very much indeed.

@justyy - the author of https://SteemYY.com and I have been a Steem Witness for more than a year now.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60265.23
ETH 2324.47
USDT 1.00
SBD 2.55