对于这个例子,让我们看一下网站的内容:
当我进行初始登录时,会有一些引脚加载。为了获得更多的引脚,我需要滚动到页面的末尾,在有更多引脚的请求之后(很多网站的工作就像我想的那样)
所以我知道如何在selenium中进行滚动,但我如何等待请求结束?
我的意思是,它不是在等待某个元素出现,那种元素(引脚)已经存在,但我在等待其他人出现。
如果我使用等待的预期条件,它对第一批引脚有好处,但是那些添加到它们的引脚,我如何等待它们,例如:
当pinterest第一次加载 - >
WebDriverWait driverWait = new WebDriverWait(cd, 10, 1000);
element = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("thepins")));
这对于初始加载非常有用,现在我滚动到页面底部
((JavascriptExecutor) cd).executeScript("window.scrollTo(0, document.body.scrollHeight)");
现在取决于页面,有一个加载到更多的引脚(有时不是)我想等待他们加载之前我做另一个滚动。
这种情况的最佳方法是什么?
我这样做的方法是使用页面上的元素数量。例如,您可以这样做:
int pinCount = webDriver.findElements(By.xpath("thepins")).size();
此时运行你的jsExecutor滚动,然后:
webDriverWait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("thepins"),pinCount ));
如果你知道总共会显示多少个引脚,你可以使用:
// new wait 30 seconds
WebDriverWait wait30s = new WebDriverWait(driver,30);
wait30s.until(ExpectedConditions.numberOfElementsToBe("THIS LOCATOR MUST FIT TO ALL PINS", number));
// or
wait30s.until(ExpectedConditions.numberOfElementsToBeMoreThan("THIS LOCATOR MUST FIT TO ALL PINS", number));
我认为处理这个问题的最好方法是让课程具备所有可能性,而不是从需要等待的地方调用它,而下面的代码将会发挥作用,对我来说效果很好
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Waiter {
private static WebDriver jsWaitDriver;
private static WebDriverWait jsWait;
private static JavascriptExecutor jsExec;
//Get the driver
public static void setDriver (WebDriver driver) {
jsWaitDriver = driver;
jsWait = new WebDriverWait(jsWaitDriver, 45);
jsExec = (JavascriptExecutor) jsWaitDriver;
}
//Wait for JQuery Load
public static void waitForJQueryLoad() {
//Wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver)
.executeScript("return jQuery.active") == 0);
//Get JQuery is Ready
boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0");
//Wait JQuery until it is Ready!
if(!jqueryReady) {
System.out.println("JQuery is NOT Ready!");
//Wait for jQuery to load
jsWait.until(jQueryLoad);
} else {
System.out.println("JQuery is Ready!");
}
}
//Wait for Angular Load
public static void waitForAngularLoad() {
WebDriverWait wait = new WebDriverWait(jsWaitDriver,45);
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";
//Wait for ANGULAR to load
ExpectedCondition<Boolean> angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver)
.executeScript(angularReadyScript).toString());
//Get Angular is Ready
boolean angularReady = Boolean.valueOf(jsExec.executeScript(angularReadyScript).toString());
//Wait ANGULAR until it is Ready!
if(!angularReady) {
System.out.println("ANGULAR is NOT Ready!");
//Wait for Angular to load
wait.until(angularLoad);
} else {
System.out.println("ANGULAR is Ready!");
}
}
//Wait Until JS Ready
public static void waitUntilJSReady() {
WebDriverWait wait = new WebDriverWait(jsWaitDriver,45);
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
//Wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) jsWaitDriver)
.executeScript("return document.readyState").toString().equals("complete");
//Get JS is Ready
boolean jsReady = (Boolean) jsExec.executeScript("return document.readyState").toString().equals("complete");
//Wait Javascript until it is Ready!
if(!jsReady) {
System.out.println("JS in NOT Ready!");
//Wait for Javascript to load
wait.until(jsLoad);
} else {
System.out.println("JS is Ready!");
}
}
//Wait Until JQuery and JS Ready
public static void waitUntilJQueryReady() {
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
//First check that JQuery is defined on the page. If it is, then wait AJAX
Boolean jQueryDefined = (Boolean) jsExec.executeScript("return typeof jQuery != 'undefined'");
if (jQueryDefined == true) {
//Pre Wait for stability (Optional)
sleep(30);
//Wait JQuery Load
waitForJQueryLoad();
//Wait JS Load
waitUntilJSReady();
//Post Wait for stability (Optional)
sleep(30);
} else {
System.out.println("jQuery is not defined on this site!");
}
}
//Wait Until Angular and JS Ready
public static void waitUntilAngularReady() {
JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
//First check that ANGULAR is defined on the page. If it is, then wait ANGULAR
Boolean angularUnDefined = (Boolean) jsExec.executeScript("return window.angular === undefined");
if (!angularUnDefined) {
Boolean angularInjectorUnDefined = (Boolean) jsExec.executeScript("return angular.element(document).injector() === undefined");
if(!angularInjectorUnDefined) {
//Pre Wait for stability (Optional)
sleep(30);
//Wait Angular Load
waitForAngularLoad();
//Wait JS Load
waitUntilJSReady();
//Post Wait for stability (Optional)
sleep(30);
} else {
System.out.println("Angular injector is not defined on this site!");
}
} else {
System.out.println("Angular is not defined on this site!");
}
}
//Wait Until JQuery Angular and JS is ready
public static void waitJQueryAngular() {
waitUntilJQueryReady();
waitUntilAngularReady();
}
public static void sleep (Integer seconds) {
long secondsLong = (long) seconds;
try {
Thread.sleep(secondsLong);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}