我的'明确等待'不起作用,但'隐含等待'有效吗?

时间:2017-01-16 10:55:24

标签: selenium selenium-webdriver webdriver

为什么我的显式等待不起作用?

  1. 我的隐式等待有效,但我的明确等待似乎没有使用指定的超时?

  2. 例如,如果我将Explicit timeout设置为300Seconds,它将恢复为隐式超时,或者如果我注释掉隐式超时,它将立即抛出错误/超时异常。

  3. 使用的代码:

    public class Base_Page extends TestListenerAdapter {
    public @FindBy(css = ".ajax_loader") WebElement ajaxLoadScreen;
    public @FindBy(css = "#preloaderSpinner") WebElement preloadSpinner;
    public WebDriver driver;
    public String packageName;
    public String className;
    public WebDriverWait wait;
    protected JavascriptExecutor jsExecutor;
    
    public Base_Page(WebDriver driver) throws Exception {
        this.driver = driver;
    
        this.wait = new WebDriverWait(this.driver, 300);
    
        this.driver.manage().window().maximize();
        this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        this.driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
        p.load(fi);
        this.browser_type = p.getProperty("browser");
        this.page_url = p.getProperty("url");
    }
    
    public void loadPage() throws Exception {
        this.driver.get(page_url);
    }
    
    public void clickMyAccount() {
        driver.findElement(By.xpath(".//*[@id='account_links']/li[1]/a2")).click();
    }
    
    public void clickHelp() {
        this.driver.findElement(By.xpath(".//*[@id='help_links']/li[1]/a")).click();
    }
    

    enter image description here

1 个答案:

答案 0 :(得分:3)

  1. 不,这不是指派Phil的问题。正如Josh发现的那样,你有定义明确的等待:

    this.wait = new WebDriverWait(this.driver, 300);

  2. 但你实际上并没有使用它。为了使用它,你需要在等待变量下面多一行(我更喜欢使用布尔值,然后根据结果决定是否点击!):

    Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='account_links']/li[1]/a2"))).isDisplayed());
    

    现在,您可以继续评论您的隐含等待。所以基本上,在你运行上面的代码之后,你的程序会等待300秒(这有点太多,但你可能有一个特殊的要求!),因为xpath中表示的元素会出现。

    Le&ts; ts假设webElement仅在4秒后出现。然后它会立即返回TRUE(这样便于不需要等待整个等待时间)。否则,它将继续轮询,直到完整的300秒过去,然后当然返回FALSE。所以你可以从那时开始操纵流程,即:

    If(elementPresent==true){
    //click element
    }
    else{
    System.out.println("Oops! Couldn't locate element!")
    }
    
    1. 实际上并没有“还原”。 (但我知道你是怎么想的),但它只是使用你的隐式等待,因为已经定义了显式等待但没有使用!。在注释隐式等待之后没有找到元素之后立即抛出异常,完全没有等待执行!
    2. 当然最好将这个等待放在一个方法中(带有可能的参数,xpathLocator和timeOut)但是现在这里有一些代码,所以你可以验证ti的工作原理:

      public void clickMyAccount() {
          WebDriver wait = new WebDriverWait(driver, 300);
      
          Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='account_links']/li[1]/a2"))).isDisplayed());
      
      
          If(elementPresent==true){
                  driver.findElement(By.xpath(".//*[@id='account_links']/li[1]/a2")).click();            
          }
          else{
          System.out.println("Oops! Couldn't locate element!")
          }
      

      希望这有帮助!