我使用简单的NodeJS脚本来测试google.com。使用了phantomJS浏览器。
var webdriver = require('selenium-webdriver');
var assert = require('assert');
var driver = new webdriver.Builder().
withCapabilities({
browserName : "phantomjs"
}).
usingServer('http://127.0.0.1:4444/wd/hub').build();
//driver.manage().timeouts().implicitlyWait(60000);
driver.get('http://www.google.com');
driver.findElement(webdriver.By.xpath('//*[@id="gbqf"]')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.getTitle().then(function(title) {
assert.equal("webdriver - Google Search", title);
});
driver.quit();
使用
运行独立的selenium服务器> java -jar selenium-server-standalone-2.35.0.jar
使用node
命令并行运行脚本。
我收到以下错误
> timers.js:103
> if (!process.listeners('uncaughtException').length) throw e;
> ^ UnknownError: Error Message => 'Unable to find element with xpath
> '//*[@id="gbqf"]'' caused by Request =>
> {"headers":{"Accept":"application/json,
> image/png","Connection":"Keep-Alive","Content-Length":"45","Content-Type":"application/json;
> charset=utf-8","Host":"localhost:17476"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//*[@id=\\\"gbqf\\\"]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/440b8b90-0fc8-11e3-8024-5bdaea20ab82/element"}
> Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12
> 15:42:01' System info: os.name: 'Linux', os.arch: 'i386', os.version:
> '2.6.18-8.el5', java.version: '1.6.0_16' Driver info: driver.version:
答案 0 :(得分:0)
页面是否有可能没有完全加载,因为我知道当我去google.com时,它会将我重定向到我当地的google.co.uk网站。
严格用于调试目的,在findElement命令之前放置5秒的睡眠。如果那样有效,请用webdriverWait替换,直到对象出现在屏幕上。
但是,我也注意到您正在将密钥发送到“form”元素。这会有用吗?您不想将密钥发送到输入字段吗?
答案 1 :(得分:0)
断言您实际上是在调用findElement
命令之前希望处于的URL上。