简体   繁体   English

在Selenium WebDriverJS中使用getWindowHandles时出错

[英]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 我正在将JavaScript和WebDriverJS与NodeJS一起使用

I am trying to switch to a new window that just opened up with a target=_blank link. 我正在尝试切换到新打开的窗口,该窗口刚刚打开了target = _blank链接。

I seem to have boiled the problem down to driver.getWindowHandles() giving me an error. 我似乎把问题归结为driver.getWindowHandles()给我一个错误。

Trimmed down Node js file: 修剪下Node js文件:

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. 如果我将var handles ...行注释掉,那么脚本将无错误结束,并将两次“ google”文本输出到命令提示符两次。

I figured it out! 我想到了!

1) The call is getAllWindowHandles in javascript. 1)调用是javascript中的getAllWindowHandles。 It drives me batty how each language api seems to have differently named methods for the same thing. 这让我胆怯,使每种语言api似乎对同一事物都具有不同命名的方法。 Reference for the webdriverJS webdriver class: http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebDriver.html webdriverJS webdriver类的参考: 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. 2)返回值是一个承诺,而不是我想要的实际数组,因此在.then语句中更容易处理。

new code that prints out: Google [array of open window names] Google 打印出的新代码:Google [打开的窗口名称数组] 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);
});

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

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