Selenium JavaScript 等待

3

我正在尝试在Node中使用selenium-webdriver来爬取Google Finance页面。但是driver.wait函数似乎没有按预期工作。我已经将我的mocha超时设置为10秒,并将driver.wait超时设置为9秒。这个测试成功了大约一半的时间,但当它失败时,它并不需要花费9秒钟才能失败 - 实际上它只需要1秒钟就会失败,然后再花8秒钟关闭测试。显然我漏掉了些什么,但我已经包含了注释掉的迭代,试图让它工作(包括setTimeout)。如果有人能帮助我看出我的思维错误,我将不胜感激。这是代码:

(function () {
    var assert = require("chai").assert;
    var webdriver = require("selenium-webdriver");
    var urlGoogleFinanceRoot = "https://www.google.com/finance";

describe("Selenium", function () {
    it("should fetch a couple of pages and keep all of the content", function (done) {
        var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
        webdriver.promise.controlFlow().on("uncaughtException", function (e) {
            console.error("Error1: " + e);
        });
        // driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
        //  return setTimeout(function () {
        //      return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
        //  }, 9000).then(function (isDisplayed) {
        //      assert.isTrue(isDisplayed);
        //      driver.quit();
        //      done();
        //  });
        // });

        // driver.wait(function () {
        //  return driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
        //      return driver.wait(function () {
        //          return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
        //      }, 9000);
        //  });
        // }, 9000).then(function (isDisplayed) {
        //  assert.isTrue(isDisplayed);
        //  driver.quit();
        //  done();
        // });
        // driver.wait(function(){
        //  return driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function(){
        //      return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
        //  });
        // },5000).then(function(isDisplayed){
        //  assert.isTrue(isDisplayed);
        //  driver.quit();
        //  done();
        // });
        driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
            driver.wait(function () {
                return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
            }, 9000).then(function (isReady) {
                assert.isTrue(isReady);
                driver.quit();
                done();
            });
            });
        });
    });
})();

以下是输出结果:

  Selenium
Error1: NoSuchElementError: no such element
  (Session info: chrome=44.0.2403.107)
  (Driver info: chromedriver=2.16.333243      (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Linux 3.16.0-4-amd64  x86_64)
    1) should fetch a couple of pages and keep all of the content


  0 passing (10s)
  1 failing

  1) Selenium should fetch a couple of pages and keep all of the content:
     Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.
1个回答

3

文档中了解到,当您提供一个函数时,它会等待promise被解析,但猜测它只运行一次,因此您需要尝试类似以下的操作:

    driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
        driver.wait(webdriver.until.elementLocated(webdriver.By.xpath("//table[@class='snap-data']")), 9000)
          .then(...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接