如何等待browser.driver.findElement返回

时间:2018-04-17 19:58:08

标签: javascript selenium testing webdriver protractor

我目前正在测试我网站上的所有元素。典型的流程是:

  1. 加载网页
  2. 查找元素
  3. 点击元素
  4. 我的设置使用switch语句选择正确的browser.driver.findElement来查找所需的元素。

    示例代码:

      switch (element_name) {
    
      case "red button":
      return browser.driver.findElement(By.id('redButton'));
      break;
    

    然后将此返回值与click()一起使用以单击元素.....

    我的问题是,似乎在网页加载后无法始终找到这些元素。我会不一致地得到错误,引用无法找到元素或元素不可见。所以我正在寻找一种方法来继续检查元素是否已加载,然后尝试单击它。我看过使用EC.visibilityOf()的示例,但看起来我无法使用browser.driver.findElement

    我已经对不同的解决方案进行了大量的研究和测试,但似乎无法为我工作。任何帮助或指导将不胜感激。

3 个答案:

答案 0 :(得分:0)

您可以使用显式等待等待,直到您在单击之前找到页面上的元素。以下是该示例代码。

WebDriverWait wait= new WebDriverWait(Driver,<time in MS>);
wait.until(elementToBeClickableAt(By.xpath("<xpath of your element>")));
element.click();

答案 1 :(得分:0)

你可以试试这个......

new WebDriverWait(driver,2000).until(ExpectedConditions.visibilityOfElementLocated(By.id('redButton')));

答案 2 :(得分:0)

var el = element(by.id('redButton'));
var EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(el), 30000).then(function(){
  return;
});