使用R Selenium处理弹出窗口

时间:2019-03-14 14:57:51

标签: r selenium xpath rselenium

我如何导航到链接,单击链接,然后从那里抓取数据?

我尝试了此代码,但没有成功。

library("RSelenium")
startServer()
mybrowser <- remoteDriver()
mybrowser$open()
mybrowser$navigate("https://finance.yahoo.com/quote/SBUX/balance-sheet?p=SBUX")
# click 'Quarterly' button...

这是很接近的事情。

现在测试更新的代码;结果如下。

> rm(list=ls())
> 
> 
> library("RSelenium")
> startServer()
Error: startServer is now defunct. Users in future can find the function in
    file.path(find.package("RSelenium"), "examples/serverUtils"). The
    recommended way to run a selenium server is via Docker. Alternatively
    see the RSelenium::rsDriver function.
> mybrowser <- remoteDriver()
> mybrowser$open()
[1] "Connecting to remote server"
Error in checkError(res) : 
  Undefined error in httr call. httr output: Failed to connect to localhost port 4444: Connection refused
> mybrowser$navigate("https://finance.yahoo.com/quote/SBUX/balance-sheet?p=SBUX")
Error in checkError(res) : 
  Undefined error in httr call. httr output: length(url) == 1 is not TRUE
> mybrowser$findElement("xpath", "//button[text() = '
+                       
+                       OK
+                       ']")$clickElement()
Error in checkError(res) : 
  Undefined error in httr call. httr output: length(url) == 1 is not TRUE
> mybrowser$findElement("xpath", "//span[text() = 'Quarterly']")$clickElement()
Error in checkError(res) : 
  Undefined error in httr call. httr output: length(url) == 1 is not TRUE
> 

1 个答案:

答案 0 :(得分:1)

我认为您可能在网站上遇到了这种情况。

enter image description here

您可以通过以下方式“点击”“确定”按钮:

mybrowser$findElement("xpath", "//button[text() = '

                            OK
                    ']")$clickElement()

然后您可以通过以下方式单击“季度”:

mybrowser$findElement("xpath", "//span[text() = 'Quarterly']")$clickElement()

(提示:要识别这些类型的错误,可以通过remDr$screenshot(TRUE)检查浏览器的当前状态会很有帮助。)

我不确定它是否是最新的,但是某些数据也可以通过API获得,您可以检查quantmod软件包以获得更轻松的访问。

完整示例:

library("RSelenium")
startServer()
mybrowser <- remoteDriver()
mybrowser$open()
mybrowser$navigate("https://finance.yahoo.com/quote/SBUX/balance-sheet?p=SBUX")
mybrowser$findElement("xpath", "//button[text() = '

                            OK
                    ']")$clickElement()
mybrowser$findElement("xpath", "//span[text() = 'Quarterly']")$clickElement()