seleniumJavascript等

我正在尝试使用Node中的selenium-webdriver来抓取Google财经页面。 driver.wait函数似乎不按预期方式工作。 我已经设置了我的摩卡超时时间为10秒,driver.wait超时时间为9秒。 testing通过了大约一半的时间,但是当失败的时候,失败的时间不会接近9秒,实际上它在1秒内失败,然后在closurestesting之前再testing8秒。 我明显错过了一些东西,但是为了使这个工作(包括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. 

从这个文档我明白,当你提供一个函数,它等待,直到承诺解决,但猜测它只运行一次,所以你得尝试这样的:

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