【问题标题】:selenium-webdriver npm waitselenium-webdriver npm 等待
【发布时间】:2014-12-03 09:22:35
【问题描述】:

我有一个使用 selenium-webdriver npm 模块执行登录的简单脚本。该脚本可以运行,但它确实很慢,并且等待超时给出了非常奇怪的结果(有时它似乎立即超时,而其他时候它等待远远超过定义的超时)。

我是否做错了什么会导致登录速度非常慢(也许通过 selenium 集线器运行)?该网站本身反应灵敏。

这是脚本:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
    usingServer('http://hubserver:4444/wd/hub').
    withCapabilities(webdriver.Capabilities.firefox()).
    build();

console.log('\n\nStarting login.');
console.log('\nConnecting to grid: http://hubserver:4444/wd/hub' );

// Load the login page and wait for the form to display
driver.get('https://testsite.com');
driver.wait(function() {
    return driver.isElementPresent(webdriver.By.name('user'));

}, 3000, '\nFailed to load login page.');

// Enter the user name
driver.findElement(webdriver.By.name('user')).sendKeys('testuser').then(function() {
    console.log("\nEntering user name");
});

// Enter the password
driver.findElement(webdriver.By.name('pass')).sendKeys('testpwd').then(function() {
    console.log("\nEntering password");
});

// Click the login button
driver.findElement(webdriver.By.id('submit')).click().then(function() {
    console.log("\nLogging in.");
});

// Wait for the home page to load
driver.wait(function() {
    console.log("\nWaiting for page to load");
    return driver.isElementPresent(webdriver.By.id('main'));
}, 3000, '\nFailed to load home page.');

driver.getCurrentUrl().then(function(url) {
    console.log("\nPage loaded: " + url);
});
driver.quit();

【问题讨论】:

  • 您没有在任何地方设置隐式等待吗? (有些人没有在他们的 SO 问题中输入他们正在使用的 exact 代码。所以我不做任何假设。)另外,您可以尝试直接驱动浏览器,而不通过集线器,看看奇怪的时间问题是否消失了? (我刚刚注意到您帖子上的日期......也许您已经解决了它并且可以发布答案?)

标签: node.js selenium


【解决方案1】:

也许您在其他地方指定了它,但在显示的代码中,您的driver.wait() 没有指定时间量。

另外,也许我误解了您的代码,因为我主要在 Python 中执行此操作,但 driver.wait(function(){}); 对我来说看起来很奇怪。这真的是正确使用 JS 绑定吗?通常,您等待找到该元素,然后调用对该元素执行某些操作的函数。我不能用JS写,而是用伪代码:

driver.wait(#element you're looking for)
# Handle exception if there is one
# Otherwise do something with element you're looking for

我也觉得

driver.isElementPresent(webdriver.By.name('user'));

应该是

driver.isElementPresent(By.name('user'));

【讨论】:

    猜你喜欢
    • 2014-04-04
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多