简体   繁体   English

Selenium-webdriver(Java Script)等待元素消失

[英]Selenium-webdriver (Java Script) wait for element to disappear

driver.wait(until.elementIsPresent(By.css(".popup-backdrop fade")), 15000);

How do I do the opposite of this?我该如何做相反的事情? I want to wait till the ".popup-backdrop fade" overlay disappears before I click on an element.我想等到“.popup-backdrop fade”覆盖消失,然后再单击一个元素。

I am using Selenium-webdriver (using Javascript and not using Java or Python or C#)我正在使用 Selenium-webdriver(使用 Javascript 而不是使用 Java 或 Python 或 C#)

Haven't found the negative wait in the code source. 没有在代码源中找到负面等待。 A solution would be to implement your own condition: 解决方案是实现自己的条件:

var webdriver = require('selenium-webdriver');
var until = webdriver.until;
var By = webdriver.By;

until.elementIsNotPresent = function elementIsNotPresent(locator) {
  return new until.Condition('for no element to be located ' + locator, function(driver) {
    return driver.findElements(locator).then(function(elements) {
      return elements.length == 0;
    });
  });
};

driver.wait(until.elementIsNotPresent(By.css(".popup-backdrop fade")), 15000);

This is another solution of the problem, more inline (but it works, as opposed to the accepted answer which crashes with "TypeError: until.Condition is not a constructor"): 这是问题的另一个解决方案,更内联(但是它有效,而不是与“TypeError:until.Condition不是构造函数”崩溃的已接受答案):

    await this._webDriver.wait(() => {
        return this._webDriver.findElements(By.id('loadingIndicator')).then(function(found) {
            return found.length === 0;
        });
    }, 3000, 'The element should disappear');

You could also try something like: 您也可以尝试以下方法:

let faderElement = webdriver.By.css('.fader');
driver.wait(webdriver.until.elementLocated(faderElement));
let faderElementFound = driver.findElement(faderElement);
driver.wait(webdriver.until.elementIsVisible(faderElementFound));
driver.wait(webdriver.until.elementIsNotVisible(faderElementFound));

As indicated on the comment to the accepted question, until.Condition is not a valid constructor in Selenium 4. Instead, one can do this: 如对已接受问题的评论所示,until.Condition不是Selenium 4中的有效构造函数。相反,可以这样做:

const { By, until, Condition } = require('selenium-webdriver');

until.elementIsNotPresent = function elementIsNotPresent(locator) {
  return new Condition('for no element to be located ' + locator, function(driver) {
    return driver.findElements(locator).then(function(elements) {
      return elements.length === 0;
    });
  });
};

The reference to using Condition can be found here: https://github.com/seleniumhq/selenium/issues/2755 可以在此处找到使用Condition的参考: https//github.com/seleniumhq/selenium/issues/2755

Good news, it's built in now好消息,它现已内置

It looks like elementIsNotVisible has been added to until after the previous answers were given.看起来elementIsNotVisible已被添加到until的答案给出之后。 I'm using selenium webdriver 4.0.0-beta.3我正在使用 selenium webdriver 4.0.0-beta.3

Check it out:一探究竟:

const timeout = 60 * 1000; // 60 seconds
const element = await driver.findElement(By.id(elementId));

// this is the important line 
await driver.wait(until.elementIsNotVisible(element), timeout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM