简体   繁体   中英

Error using getWindowHandles in Selenium WebDriverJS

There is a question very similar to this asking how to do what I want to do, but the answer is not working for me. I do not have enough reputation to comment or ask for clarification on that yet.

I am using JavaScript and WebDriverJS with NodeJS

I am trying to switch to a new window that just opened up with a target=_blank link.

I seem to have boiled the problem down to driver.getWindowHandles() giving me an error.

Trimmed down Node js file:

var webdriver = require("selenium-webdriver");
var driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

driver.get('https://www.google.com');
driver.getTitle().then(function (title) {
    console.log(title);
    var handles = driver.getWindowHandles();
});
driver.getTitle().then(function (title) {
    console.log(title);
});

This is what my command line looks like:

C:\selenium>node test2.js
Google
C:\selenium\node_modules\selenium-webdriver\lib\goog\async\nexttick.js:39
  goog.global.setTimeout(function() { throw exception; }, 0);
                                            ^
TypeError: undefined is not a function
    at C:\selenium\test2.js:8:23
    at promise.ControlFlow.runInFrame_ (C:\selenium\node_modules\selenium-webdri
ver\lib\webdriver\promise.js:1877:20)
    at promise.Callback_.goog.defineClass.notify (C:\selenium\node_modules\selen
ium-webdriver\lib\webdriver\promise.js:2464:25)
    at promise.Promise.notify_ (C:\selenium\node_modules\selenium-webdriver\lib\
webdriver\promise.js:563:12)
    at Array.forEach (native)
    at Object.goog.array.forEach (C:\selenium\node_modules\selenium-webdriver\li
b\goog\array\array.js:203:43)
    at promise.Promise.notifyAll_ (C:\selenium\node_modules\selenium-webdriver\l
ib\webdriver\promise.js:552:16)
    at goog.async.run.processWorkQueue (C:\selenium\node_modules\selenium-webdri
ver\lib\goog\async\run.js:125:21)
    at runMicrotasksCallback (node.js:337:7)
    at process._tickCallback (node.js:355:11)

If I comment out the var handles... line then the script finishes with no error and prints the text "google" twice to the command prompt.

I figured it out!

1) The call is getAllWindowHandles in javascript. It drives me batty how each language api seems to have differently named methods for the same thing. Reference for the webdriverJS webdriver class: http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebDriver.html

2) The return is a promise, not the actual array I wanted, so it is easier to handle in a .then statement.

new code that prints out: Google [array of open window names] Google

var webdriver = require("selenium-webdriver");
var driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

driver.get('https://www.google.com');
driver.getTitle().then(function (title) {
    console.log(title);
    driver.getAllWindowHandles().then(function (allhandles) {
        console.log(allhandles);
    });
});
driver.getTitle().then(function (title) {
    console.log(title);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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