使用R Selenium来处理弹出窗口

问题描述 投票:0回答:1

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

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

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
> 
r selenium xpath rselenium
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.