无法使用RSelenium包,函数rsDriver()表示无法连接到浏览器

bnlyeluc  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(112)

所以,我以前从未使用过Selenium,直接使用了R的RSelenium包。
我已经安装并加载了这个包,但无法通过调用rsDriver()函数。
我遇到的第一个问题是我的Chrome版本不是最新的,所以我更新了它。之后,每次我调用这个函数,它都无法连接到服务器,并说浏览器没有重新启动。Chrome、Firefox和Internet Explorer都会出现这种情况,只是措辞略有变化。让我给你演示一下:

> rsDriver(browser="firefox")
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
Error in wdman::selenium(port = port, verbose = verbose, version = version,  : 
  Selenium server signals port = 4567 is already in use.

> rsDriver(browser="internet explorer")
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
Error in wdman::selenium(port = port, verbose = verbose, version = version,  : 
  Selenium server signals port = 4567 is already in use.

> rsDriver()
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
[1] "Connecting to remote server"
Could not open chrome browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 4567: Connection refused
Check server log for further details.
$client
[1] "No sessionInfo. Client browser is mostly likely not opened."

$server
PROCESS 'file964483c2661.bat', running, pid 8384.
Warning message:
In rsDriver() : Could not determine server status.

我已经安装和卸载Chrome并重新启动计算机几次。它确实改变了错误的措辞,但不是那么多。我尝试设置不同的端口几次(4444L),但没有帮助:它要么说端口也在使用中,要么说无论如何都无法连接到浏览器。我不知道出了什么问题以及如何修复它。
我在PC上使用RStudio版本1.3.1093,Windows 7家庭基本操作系统,64x。任何帮助都非常感谢。谢谢。

klsxnrf1

klsxnrf11#

您可以考虑以下两种方法:

library(RSelenium)

shell('docker run -d -p 4446:4444 selenium/standalone-firefox') # Docker has to be installed
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4446L, browserName = "firefox")
remDr$open()
remDr$navigate("https://www.google.com")

library(RSelenium)
library(wdman)

port <- as.integer(4444L + rpois(lambda = 1000, 1))
pJS <- wdman::phantomjs(port = port)
remDr <- remoteDriver(browserName = "phantomjs", port = port)
remDr$open()
remDr$navigate("https://www.google.com")

相关问题