我有一个简单的脚本,它使用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();发布于 2014-12-03 09:22:35
也许您在其他地方指定了它,但在显示的代码中,您的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'));https://stackoverflow.com/questions/25139905
复制相似问题