RSelenium:单击另一个链接中的链接

RSelenium:单击另一个链接中的链接,r,selenium,web-scraping,dplyr,rselenium,R,Selenium,Web Scraping,Dplyr,Rselenium,我有这个RSelenium脚本: library(tidyverse) library(RSelenium) # running through docker library(rvest) library(httr) remDr <- remoteDriver(port = 4445L, browserName = "chrome") remDr$open() remDr$navigate("https://books.google.com/") books <- remDr$

我有这个
RSelenium
脚本:

library(tidyverse)
library(RSelenium) # running through docker
library(rvest)
library(httr)

remDr <- remoteDriver(port = 4445L, browserName = "chrome")
remDr$open()


remDr$navigate("https://books.google.com/")
books <- remDr$findElement(using = "css", "[name = 'q']")

books$sendKeysToElement(list("NHL teams", key = "enter"))

bookElem <- remDr$findElements(using = "xpath",
                               "//h3[@class = 'LC20lb']//parent::a")

links <- sapply(bookElem, function(bookElem){
  bookElem$getElementAttribute("href")
})
或者我尝试一个
for
循环而不是
sapply
,我得到
错误:$运算符对原子向量无效

for(link in links) {

  # Navigate to each link
  remDr$navigate(link)

  # If statement to get past book previews
  if (str_detect(link, "frontcover")) {

    link2 <- remDr$findElement(using = 'xpath',
       '//a[@id="sidebar-atb-link" and span[.="About this book"]]')

     for(i in length(link2)){
      i$getElementAttribute('href')
     }

    } else {
     print("dumbass")
   }
}
for(链接中的链接){
#导航到每个链接
remDr$navigate(链接)
#If语句以通过书本预览
如果(str_detect(链接,“封面”)){

link2只需更新下一行即可

aboutLinks <- remDr$findElements(using = 'xpath', 
                           '//a[@id="sidebar-atb-link" and span[.="About this book"]]')
links2 <- sapply(aboutLinks, function(about_link){
  about_link$getElementAttribute('href')
})

aboutLinks仍然会出现令人困惑的
Error:type'closure'的对象不是subttable
Error,很不幸。一定是与以下
sapply
函数有关吗?正如中所述,您的元素肯定是正确的,但之后的某个元素不起作用
link2我在某个地方读到了可能给我的信息那个错误..但谁知道呢.同样的错误-同样的错误.可能是我的机器吗?不过,如果我注释掉
sapply
部分和
print(link2)
,它确实打印出了正确的链接数。只有
sapply
抛出了子可连接错误OK,解决了问题。我们在获取有关本书链接的
时错过了
s'。我们应该将元素传递给
sapply
,而不是元素。
aboutLinks <- remDr$findElements(using = 'xpath', 
                           '//a[@id="sidebar-atb-link" and span[.="About this book"]]')
links2 <- sapply(aboutLinks, function(about_link){
  about_link$getElementAttribute('href')
})