java 等待页面在 Selenium 中完全加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42230242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for a page to fully load in Selenium
提问by user6939
I am using selenium with Java. I want to wait for page to load fully before doing any action on that page.
我在 Java 中使用硒。在对该页面执行任何操作之前,我想等待页面完全加载。
I have tried the following method, but it is failing to work as expected.
我尝试了以下方法,但未能按预期工作。
public void waitForElementToBeVisible(final WebElement element) {
WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME);
wait.until(ExpectedConditions.visibilityOf(element));
回答by Atul
WebDriverWaitinherits methods like wait until.
WebDriverWait继承了诸如 wait until 之类的方法。
So something like
所以像
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated( elementLocator)
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated( elementLocator)
should work. You can use ExpectedConditions, it would make things simpler. You can also use the method visibilityOfAllElements
应该管用。您可以使用ExpectedConditions,它会使事情变得更简单。您还可以使用方法visibilityOfAllElements
回答by Eddie Singh
This method will wait until element is visible. Firstly this method will check, whether element is available in html and whether it's display.. it will wait until element will display..
此方法将等到元素可见。首先这个方法会检查元素在html中是否可用以及它是否显示..它会等到元素显示..
public void E_WaitUntilElementDisplay() throws Exception
{
int i=1;
boolean eleche,eleche1 = false;
while(i<=1)
{
try{
eleche = driver.findElements(by.xpath("path")).size()!=0;
}catch(InvalidSelectorException ISExcep)
{
eleche = false;
}
if(eleche == true)
{
while(i<=1)
{
try{
eleche1=driver.findElement(By.xpath("Path")).isDisplayed();
}catch(org.openqa.selenium.NoSuchElementException NSEE){
eleche1=false;
}
if(eleche1 == true)
{
i=2;
System.out.println("\nElement Displayed.");
}
else
{
i=1;
Thread.sleep(1500);
System.out.println("\nWaiting for element, to display.");
}
}
}
else
{
i=1;
Thread.sleep(1500);
System.out.println("\nWaiting for element, to display.");
}
}
}
回答by ferpel
As another option maybe you can try something like:
作为另一种选择,也许您可以尝试以下操作:
if(element.isDisplayed() && element.isEnabled()){
//your code here
}
or if you know how long you want to wait:
或者如果您知道要等多久:
thread.sleep(3000); //where 3000 is time expression in milliseconds, in this case 3 secs
回答by vkrams
You can use this function in java to verify whether the page is fully loaded or not. The verification happens two-fold. One using the javascript document.readystate and imposing a wait time on javascript.
你可以在java中使用这个函数来验证页面是否完全加载。验证分为两部分。一种使用 javascript document.readystate 并在 javascript 上强加等待时间。
/* Function to wait for the page to load. otherwise it will fail the test*/
public void waitForPageToLoad() {
ExpectedCondition<Boolean> javascriptDone = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
try {
return ((JavascriptExecutor) getDriver()).executeScript("return document.readyState").equals("complete");
} catch (Exception e) {
return Boolean.FALSE;
}
}
};
WebDriverWait wait = new WebDriverWait(getDriver(), waitTimeOut);
wait.until(javascriptDone);
}
回答by Gajanan
This works for me:
这对我有用:
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[contains(text(),'Loading...')]"))));