【发布时间】:2014-11-10 13:03:47
【问题描述】:
我目前正在使用 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 错误还是我在实现中犯的错误?
【问题讨论】:
-
不知道页面是无法回答的。您是否尝试过将用户代理设置为Chrome 13 并将视口设置为类似桌面的东西,因为某些站点会根据大小提供不同的标记。您是否通过截屏或打印源代码来验证您在正确的页面上?
-
我知道我在正确的页面上,因为一些基本测试通过了。我没有想到视口的事情,但这似乎没有什么不同。不过,我对“Chrome 13”用户代理的事情有点困惑。我该如何应用?
-
也希望你不介意我回滚你对我的标题的编辑。我想在其中提及 WebdriverIO/JS,因为我不确定它是否会导致问题。
-
对了,你有试过提高logLevel吗?你确定没有任何错误?我不知何故怀疑这是一个 webdriver-io 问题。
-
我已将 logLevel 设置为“verbose”,这会产生上面找到的错误消息。不确定该选项是否有任何进一步的设置。不要以为您自己能够重现此问题?
标签: node.js selenium-webdriver phantomjs ghostdriver webdriver-io