coderLMN / AutomatedDataCollectionWithR

《基于 R 语言的自动化数据采集技术》读者讨论区
28 stars 10 forks source link

P208初试Selenium Webdriver总是报错,求指导 #8

Open dayushan opened 7 years ago

dayushan commented 7 years ago

image image image image image

在初试化服务器的时候,一开始会显示Selenium server is up and running. 而后就报错

library(devtools) install_github(repo="Rwebdriver",username="crubba") .libPaths("D:/R/library") library(Rwebdriver) library(XML)

创建一个新的浏览器窗口,告诉服务器要产生的是一个FireFox浏览器窗口

start_session(root="http://localhost:4444/wd/hub/",browser="firefox") image

coderLMN commented 7 years ago

看了一下你贴的错误信息:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

貌似是 firefox 驱动的问题,然后上网查了一下,看到了这个链接 里的解释:

Marionette (the next generation of FirefoxDriver) is turned on by default from Selenium 3. Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

Note: If you are using Selenium version below 2.53, you don't need gecko additional driver.

If you are not doing so, it will throw exception "java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;"

看来是你用的 Selenium v3 需要这个驱动。另一个简单的办法是用低于 v2.53 版本的 Selenium 。我翻译的时候用的就是 v2.53 ,没有遇到你的这个问题。

dayushan commented 7 years ago

第一,点开selenium server standalone 下载2.53.0版本用不了 第二、下载了两个驱动版本都用不了, image 此外https://www.seleniumeasy.com/selenium-tutorials/launching-firefox-browser-with-geckodriver-selenium-3里说的这个操作我也不知道在哪执行? 如果老师有以前的selenium server 版本还麻烦发我一下哈。没有的话看看有没有可用的驱动版本,并教教我怎么设置。我是这方面的小白,麻烦老师费心指导啦 Thanks

coderLMN commented 7 years ago

可以在官网下载这个 v2.53.1 版,亲测有效。

我刚刚还在本书的 GitHub Page 上放了一个我以前下载过的 v2.53.0 版,也可以用这个。

dayushan commented 7 years ago

在下载v2.53.0后,能初始化服务器,显示Selenium server is up and running。在用start_session()后也能打开火狐浏览器窗口,但是一会儿就报错: image image image image

image image image

coderLMN commented 7 years ago

这里的关键错误信息是这一条:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms.

我去查了一下这个错误,貌似是因为你的 Firefox 的版本(48.0.2)和 Selenium 2.53.0 版本不兼容。这两个软件之间不同版本的兼容性问题一直广为诟病。

参考这篇文章 里说的,对应 Selenium 2.53 ,Firefox 47.0.1 应该是能工作的,你可以在 Mozilla 官方网站 下载它。如果还不行,你也可以试试把 Firefox 升级到现在最新的 49.2 试试。

dayushan commented 7 years ago

老师,下载FireFox 47.0.1版本仍然报上边那个错误,不兼容。下载49.2.0也报错,好像也是不兼容的问题,错误如下: image image image image image image image

dayushan commented 7 years ago

老师,版本不兼容问题已经解决,用selenium v2.53.1匹FireFox_47.0.1.6018. 虽然最后数据提取出来了,但是用window_change(allHandles[1])时报错Error in rawToChar(getURLContent(paste0(seleniumSession$sessionURL, "/window"), : 参数'x'必需是矢量 还是向请问下老师这个是什么原因呢 有没有改进方法

coderLMN commented 7 years ago

这个问题比较少见,我没查到原因。上次碰到一个 iFrame 的问题,发现书里面这个 Rwebdriver 组件是该书的作者之一 C. Rubba 编写的,我个人觉得它做得并不好,而且自从三年前提交后就没有再更新过,文档也基本空白。后来我就改用 RSelenium 组件 (可以参考这个 讨论),不再用它了。你这次碰到这个问题,我怀疑和 Rwebdriver 组件也有关系,所以我推荐你也换成 RSelenium 。它的文档可以参阅:https://cran.r-project.org/web/packages/RSelenium/vignettes/RSelenium-basics.html


另外我今天还试了一下 Selenium 3.0.1 版,需要先下载 geckodriver驱动,解压到某个路径(比如 ./Downloads ),然后在运行 Selenium 的命令行里加上这个路径作为 webdriver.gecko.driver 参数即可:

java -Dwebdriver.gecko.driver=./Downloads/geckodriver -jar Downloads/selenium-server-standalone-3.0.1.jar