使用 R Selenium 处理弹窗

Use R Selenium to deal with pop-ups

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

我试过这段代码,没有成功。

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
> 

我认为您 运行 在网站上可能是这种情况。

您可以 "click" OK 按钮通过:

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

                            OK
                    ']")$clickElement()

然后您可以通过以下方式点击"quarterly":

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()