phantomjs 无法在页面上找到元素

phantomjs unable to find element on page

最近,我在 RSelenium 下驱动 phantomjs 遇到了问题。浏览器似乎无法使用 findElement() 在页面上定位任何内容。如果我传递一些简单的东西:

library("RSelenium")
RSelenium::checkForServer()
RSelenium::startServer()
rd <- remoteDriver(browserName = "phantomjs")
rd$open()
Sys.sleep(5)

rd$navigate("https://www.Facebook.com")
searchBar <- rd$findElement(using = "id", "email")

我收到以下错误:

Error:   Summary: NoSuchElement
Detail: An element could not be located on the page using the given search parameters.
class: org.openqa.selenium.NoSuchElementException

想知道是什么原因造成的吗?我导航到哪个页面似乎并不重要;每当我尝试在网页上定位元素时,它都会失败。这个问题最近开始出现,当我的 cron 作业开始失败时我注意到了它。

我正在 Ubuntu 14.04 LTS 中使用 R 3.3.1 和 phantomjs 2.1.1。我不怀疑某种类型的兼容性问题,因为这最近才起作用,而且我还没有更新任何东西。

您安装的 phantomjs 版本可能受到限制。参见 here

  • Disabled Ghostdriver due to pre-built source-less Selenium blobs.
  • Added README.Debian explaining differences from upstream "phantomjs".

如果您最近使用 apt-get 安装,那么很可能是这种情况。您可以从 phantomjs website 下载并将 bin 位置放在您的 PATH 中。

或者使用npm为您安装一个版本

npm install phantomjs-prebuilt

这将只是 link 到 node_modules/.bin/phantomjs 中的垃圾箱。

关于 apt-get 限制背后的原因,您可以阅读包含 here 的 README.Debian 文件。

Limitations

Unlike original "phantomjs" binary that is statically linked with modified QT+WebKit, Debian package is built with system libqt5webkit5. Unfortunately the latter do not have webSecurity extensions therefore "--web-security=no" is expected to fail.

https://github.com/ariya/phantomjs/issues/13727#issuecomment-155609276


Ghostdriver is crippled due to removed source-less pre-built blobs:

src/ghostdriver/third_party/webdriver-atoms/*

Therefore all PDF functionality is broken.


PhantomJS cannot run in headless mode (if there is no X server available).

Unfortunately it can not be fixed in Debian. To achieve headless-ness upstream statically link with customised QT + Webkit. We don't want to ship forks of those projects. It would be great to eventually convince upstream to use standard libraries. Meanwhile one can use "xvfb-run" from "xvfb" package:

xvfb-run --server-args="-screen 0 640x480x16" phantomjs

如果您不想为 phantomjs 设置路径,那么您可以将其添加为额外的:

library(RSelenium)

selServ <- startServer()
pBin <- list(phantomjs.binary.path = "/home/john/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs")
rd <- remoteDriver(browserName = "phantomjs"
                   , extraCapabilities = pBin)
Sys.sleep(5)
rd$open()

rd$navigate("https://www.Facebook.com")
searchBar <- rd$findElement(using = "id", "email")

rd$close()
selServ$stop()