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')
})