[英]Error using getWindowHandles in Selenium WebDriverJS
有一个与此问题非常相似的问题,询问如何做我想做的事情,但答案对我不起作用。 我没有足够的声誉来对此发表评论或要求澄清。
我正在将JavaScript和WebDriverJS与NodeJS一起使用
我正在尝试切换到新打开的窗口,该窗口刚刚打开了target = _blank链接。
我似乎把问题归结为driver.getWindowHandles()给我一个错误。
修剪下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);
});
这是我的命令行的样子:
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)
如果我将var handles ...行注释掉,那么脚本将无错误结束,并将两次“ google”文本输出到命令提示符两次。
我想到了!
1)调用是javascript中的getAllWindowHandles。 这让我胆怯,使每种语言api似乎对同一事物都具有不同命名的方法。 webdriverJS webdriver类的参考: http ://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebDriver.html
2)返回值是一个承诺,而不是我想要的实际数组,因此在.then语句中更容易处理。
打印出的新代码: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.