1

我正在使用简单的 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:
4

2 回答 2

0

是否有可能该页面尚未完全加载,因为我知道当我访问 google.com 时,它会将我重定向到我的本地 google.co.uk 站点。

严格来说,出于调试目的,请在 findElement 命令之前休眠 5 秒。如果这样可行,请替换为 webdriver 等待对象出现在屏幕上。

但是,我也注意到您将密钥发送到“表单”元素。这行得通吗?您不想将键发送到输入字段吗?

于 2013-08-28T11:06:59.137 回答
0

findElement在调用命令之前,断言您实际上是在您期望的 URL 上。

于 2014-08-18T19:51:22.897 回答