【问题标题】:Selenium Javascript Get title from GoogleSelenium Javascript 从 Google 获取标题
【发布时间】:2018-03-26 17:13:30
【问题描述】:

我想做的只是一些非常简单的事情,但我很难做到。我想从谷歌检索标题并使用 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] 行在完成从网站获取标题之前执行。

谢谢

【问题讨论】:

    标签: javascript selenium driver


    【解决方案1】:

    也许这会对你有所帮助。

    首先你需要构建你的驱动程序:

    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 语法而不是 Promise 来使测试更加清晰和简短。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2021-09-04
      相关资源
      最近更新 更多