【发布时间】: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 代码。所以我不做任何假设。)另外,您可以尝试直接驱动浏览器,而不通过集线器,看看奇怪的时间问题是否消失了? (我刚刚注意到您帖子上的日期......也许您已经解决了它并且可以发布答案?)