Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java在selenium webdriver中放置driver.wait的有效方法_Java_Selenium_Selenium Webdriver - Fatal编程技术网

使用java在selenium webdriver中放置driver.wait的有效方法

使用java在selenium webdriver中放置driver.wait的有效方法,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有这样的元素。我想将thread.sleep放在中间或单击两下等待时间。目前我正在将其硬编码为Thread.sleep(4000)。请帮助我以有效的方式书写,或者更确切地说,这不是放置Thread.sleep的正确方式 如果有,您可以在此处使用隐式等待,这是一个很好的练习 单击中的某些元素,然后可以使用fluentwait 在给定的时间间隔内轮询元素,以获取您可以了解的信息 示例代码: public class menupageProperty{ public static WebEle

我有这样的元素。我想将thread.sleep放在中间或单击两下等待时间。目前我正在将其硬编码为Thread.sleep(4000)。请帮助我以有效的方式书写,或者更确切地说,这不是放置Thread.sleep的正确方式

如果有,您可以在此处使用隐式等待,这是一个很好的练习 单击中的某些元素,然后可以使用fluentwait 在给定的时间间隔内轮询元素,以获取您可以了解的信息

示例代码:

public class menupageProperty{
  public static WebElement administrativeModule(WebDriver driver) {
   private static WebElement menuElement= null; 
   menuElement= driver.findElement(By.linkText("ADMINISTRATIVE MODULE"));
   return menuElement;
  }
  public static WebElement editModule(WebDriver driver) {
   private static WebElement menuElement= null; 
   menuElement= driver.findElement(By.linkText("Edit MODULE"));
   return menuElement;
  }
}

public class runAutomation {
  public static void main(String[] args) throws IOException, InterruptedException{
    menupageProperty.administrative_Module(driver).click();
    Thread.sleep(2000);
    menupageProperty.userMgmtClick(driver).click();
    Thread.sleep(4000);
    menupageProperty.edituser(driver).click();
 }
}

下面是一个方法,它将等待元素显示在DOM中

Ps:公开驱动程序并将其作为参数传递不是一个好的做法

// Waiting 30 seconds for an element to be present on the page, checking

  // for its presence once every 5 seconds.

  Wait wait = new FluentWait(driver)

    .withTimeout(30, SECONDS)

    .pollingEvery(5, SECONDS)

    .ignoring(NoSuchElementException.class);

  WebElement foo = wait.until(new Function() {

    public WebElement apply(WebDriver driver) {

    return driver.findElement(By.id("foo"));

    }

   });

我想推荐如下:

public void waitIfNotPresent(WebElement element) {
        webElementWait.until(driver -> {
            try {
                return !element.isDisplayed();
            }
            catch (NoSuchElementException e) {
                return true;
            }
        });

在下一次单击时传入您正在等待的元素,并等待其可单击。这将消除对每个时间进行硬编码的需要,您可以依赖于指定的webDriverWait时间

最简单的方法是使用
等待
加载元素

void waitForElementToLoad(WebElement webElement) {
    WebElement element = webDriverWait.until(ExpectedConditions.elementToBeClickable(webElement));
}
但是,如果您想等到某个ajax请求完成后再使用

  WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='Welcome']")));
newwebdriverwait(驱动程序,180)。直到(new ExpectedCondition()
{公共布尔应用(WebDriver驱动程序)
{JavascriptExecutor js=(JavascriptExecutor)驱动程序;
return(Boolean)js.executeScript(“return jQuery.active==0”);
}
}); 

只需使用
驱动程序。wait
方法,它将等待元素加载。这是一个页面中的几个元素,我有大约48页的web元素。我该如何明确地说Driver.wait?等x秒?我知道加载每个web元素大约需要4-7秒。但在我的代码中,我不想硬编码驱动程序。请在两次单击Google“Selenium+Driver.wait”之间等待(x)秒,然后查看文档和示例。只需使用fluentwait,它将一直等待,直到上面的代码示例中没有出现的项目。你能在这里给我一些示例代码吗?基于上面的代码示例,你能在这里给我一些示例代码吗?你建议如何避免像你在评论中所说的那样暴露驱动程序。目前,我正在做的是,我为每个页面设置了对象库,这是我们通过检查网页收集信息的一种方式。如上面的问题所示,在主类中使用该选项单击。你们能帮我过来吗?因为我正处于构建自动化框架的初始阶段。在一个类中使用它会很有帮助,只需调用此类,并使用页面模式。如您在评论中所说,您建议如何避免暴露驱动程序。目前,我正在做的是,我为每个页面设置了对象库,这是我们通过检查网页收集信息的一种方式。如上面的问题所示,在主类中使用该选项单击。你们能帮我过来吗?因为我正处于构建自动化框架的初始阶段。会有很大帮助的
new WebDriverWait(driver, 180).until(new  ExpectedCondition<Boolean>() 
{ public Boolean apply(WebDriver  driver) 
{ JavascriptExecutor js = (JavascriptExecutor) driver;
 return (Boolean) js.executeScript("return jQuery.active == 0"); 
}

});