Java 等待页面在Selenium中完全加载
我正在使用selenium和Java。我想等待页面完全加载,然后再对该页面执行任何操作 我试过下面的方法,但没有达到预期效果Java 等待页面在Selenium中完全加载,java,selenium,Java,Selenium,我正在使用selenium和Java。我想等待页面完全加载,然后再对该页面执行任何操作 我试过下面的方法,但没有达到预期效果 public void waitForElementToBeVisible(final WebElement element) { WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME); wait.until(Expec
public void waitForElementToBeVisible(final WebElement element) {
WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME);
wait.until(ExpectedConditions.visibilityOf(element));
此方法将等待元素可见。 首先,该方法将检查元素在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.");
}
}
}
public void E_WaitUntilElementDisplay()引发异常
{
int i=1;
布尔eleche,eleche1=false;
而(i继承了wait等方法
大概是
webDriverWait.until(预期条件。元素定位符的可见性)
应该可以。你可以使用,它会让事情变得更简单。你也可以使用这个方法
作为另一种选择,您可以尝试以下方式:
if(element.isDisplayed() && element.isEnabled()){
//your code here
}
或者,如果您知道要等待多长时间:
thread.sleep(3000); //where 3000 is time expression in milliseconds, in this case 3 secs
您可以在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);
}
/*函数等待页面加载。否则它将无法通过测试*/
public void waitForPageToLoad(){
ExpectedCondition javascriptDone=新的ExpectedCondition(){
公共布尔应用(WebDriver d){
试一试{
return((JavascriptExecutor)getDriver()).executeScript(“return document.readyState”).equals(“complete”);
}捕获(例外e){
返回Boolean.FALSE;
}
}
};
WebDriverWait wait=新建WebDriverWait(getDriver(),waitTimeOut);
等到(javascriptDone);
}
这对我很有用:
wait.until(ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[contains(text(),'Loading...')]"))));
这里是最终的解决方案,特别是当你处理角度7或8
您可以将等待时间划分为分区并递归使用,而不是使用sleep或隐式等待方法等待更长的时间
下面的逻辑将等待页面呈现至少300秒,最多900秒
/**
* This method will check page loading
*
*/
public void waitForLoadingToComplete() {
waitLoadingTime(3); // Enter the number of attempts you want to try
}
private void waitLoadingTime(int i) {
try {
// wait for the loader to appear after particular action/click/navigation
this.staticWait(300);
// check for the loader and wait till loader gets disappear
waitForElementToBeNotPresent(By.cssSelector("Loader Element CSS"));
} catch (org.openqa.selenium.TimeoutException e) {
if (i != 0)
waitLoadingTime(i - 1);
}
}
/**
* This method is for the static wait
*
* @param millis
*/
public void staticWait(final long millis) {
try {
TimeUnit.MILLISECONDS.sleep(millis);
} catch (final InterruptedException e) {
System.err.println("Error in staticWait." + e);
}
}
public void waitForElementToBeNotPresent(final By element) {
long s = System.currentTimeMillis();
new WebDriverWait(this.getDriver(), 30)
.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(element)));
System.err.println("Waiting for Element to be not present completed. " + (System.currentTimeMillis() - s));
}
这可能是相关的:该元素是否可能重复是页面上最后一个要加载的内容?页面上是否存在动态事件,使其在所有内容似乎都已加载后继续移动?您是否发现元素不存在的错误?或者元素已过时?否我要单击的元素不存在I don’我不想加载,但我想最好在加载完成后再做任何操作。如果我错了,请纠正我,因为我是自动化新手,如果有人能提供好的链接来了解更多关于java的内容,硒将不胜感激。谢谢澄清问题。希望..我的回答能帮助你..或者让你接近答案..谢谢Eddie,印度。