Selenium Javascript从Google获取标题

时间:2018-03-26 17:13:31

标签: javascript selenium driver

我想要做的只是一件非常简单的事情,但我遇到了这么艰难的时期。我想从Google检索标题并使用Javascript和驱动程序进行打印。

这是我的代码:

const {Builder, By, Key, until} = require('selenium-webdriver');
var driver = new Builder().forBrowser('internet explorer').build();
driver.get("https://www.google.com/");
var title = driver.executeScript("return document.title;");
console.log("Title: " + title);

这是我的错误:

Title: [object Promise]                                                                                                         
(node:5412) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): UnsupportedOperationError: Error 404: Not Found                                                                                                    
Not Found                                                                                                               
(node:5412) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code. 

标题:[object Promise]行在完成从网站获取标题之前执行。

由于

1 个答案:

答案 0 :(得分:0)

也许这会对您有所帮助。

首先,您需要构建驱动程序:

let capabilities = webdriver.Capabilities.ie();
var driver = new webdriver.Builder().withCapabilities(capabilities).build();

然后随便做什么

async function () {
   await driver.get("https://www.google.com");
}

async function () {
   var title = await driver.getTitle();
   assert.equal(title, "Google");
});

我使用async \ await语法代替了使测试更加清晰简短的承诺。