我目前正在使用 WebdriverJS 和 PhantomJS 编写应用程序测试套件。为了确保我的测试正常工作,我首先通过 Chrome 运行它们,它们都运行良好。但是,当我将 Chrome 换成 PhantomJS 时,测试就中断了。
这个问题 - WebDriver PhantomJS Unable to find element, but works fine with Firefox - 似乎概述了一个非常相似的问题,但随附的解决方案似乎没有帮助。
这是在 Chrome 上工作的东西类型的粗略示例,但在 PhantomJS 上不工作:
var client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'chrome'
},
logLevel: 'silent'
});
client.waitForExist("[data-id='1568911']", function(e){
client.click("[data-id='1568911']", function(e){
assert(!e, "Should click on a specific element:" + element);
});
});
在 PhantomJS 上运行时,我显然首先更改了 WebdriverJS 选项:
var client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'phantomjs',
'phantomjs.binary.path': "path/to/phantomjs"
},
logLevel: 'silent'
});
但是当我运行测试并将 logLevel 设置为“详细”时,我收到如下错误消息:
[12:43:34]: COMMAND POST "/wd/hub/session/eb2b0a4b-e659-4607-bec0-82209bd6539a/element"
[12:43:34]: DATA {"using":"css selector","value":"[data-id='1568911']"}
[12:43:35]: ERROR UnknownError An unknown server-side error occurred while processing the command.
{"errorMessage":"Unable to find element with css selector '[data-id='1568911']'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"54","Content-Type":"application/json; charset=utf-8","Host":"localhost:12784","User-Agent":"Apache-HttpClient/4.3.2 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"css selector\",\"value\":\"[data-id='1568911']\"}","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/2e1ff0a0-68d7-11e4-ad4c-3105ad572a89/element"}}
为什么常见的 CSS2+ 选择器,如“[data-id='1568911']”,甚至“#foo”,不能通过 WebdriverJS 在 PhantomJS 上工作?它是 PhantomJS 错误、WebdriverJS 错误还是我在实现中犯的错误?