【问题标题】:My 'Explicit Wait' doesn't work but 'Implicit Wait' Works?我的“显式等待”不起作用,但“隐式等待”有效?
【发布时间】:2017-01-16 10:55:23
【问题描述】:

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

  1. 我的隐式等待有效,但我的显式等待似乎没有使用分配的超时时间?

  2. 例如,如果我将显式超时设置为 300 秒,它将恢复为隐式超时,或者如果我注释掉隐式超时,它将立即引发错误/超时异常。

使用的代码:

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();
}

【问题讨论】:

  • 将您的代码显示为文本而不是图像
  • @Andersson 全部完成
  • 你在哪里使用显式等待?
  • 嗨@Josh,我附上了一张图片,显示了我在哪里使用了显式等待
  • 你在哪里使用 clickMyAccount() ?

标签: selenium selenium-webdriver webdriver


【解决方案1】:
  1. 不,这不是分配给菲尔的问题。正如 Josh 正确发现的那样,您在此处定义显式等待:

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

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

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

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

让我们假设 webElement 确实在 4 秒后出现。然后它将立即返回 TRUE(这很方便,不需要等待整个等待时间)。否则,它将继续轮询,直到整整 300 秒过去,然后当然返回 FALSE。因此,您可以从那时起操纵流程,即:

If(elementPresent==true){
//click element
}
else{
System.out.println("Oops! Couldn't locate element!")
}
  1. 它实际上并不是“还原”(但我知道你是怎么想的),它只是使用你的隐式等待,因为显式等待已定义但未使用!。它在没有找到元素后立即抛出异常,因为在评论了隐式等待之后,根本没有强制执行任何等待!

当然,最好将此等待保留在一个方法中(使用可能的参数,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!")
    }

希望这会有所帮助!

【讨论】:

  • 再次感谢您的帮助,您认为在同一个项目中同时设置隐式和显式等待设置是否可取?
  • 嗨,菲尔,如果我的回答确实有帮助,请按向上箭头进行投票。就个人而言,不,我认为在您定义了显式等待之后不需要隐式等待(一个正在取消另一个)。如前所述,您可以创建一个名为 waitForElement( String Xpath, int timeOut) 的新方法,并根据您的需要传递 xpath 和等待时间。这是为了避免一直打字,显式wait + wait.until等的定义
猜你喜欢
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
相关资源
最近更新 更多