RSelenium:在另一个 link 中单击 link
RSelenium: clicking a link within another link
我有这个 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")
})
以上在 Google 搜索 return 中每 link 次点击(每页有 10 次)。单击进入后,我搜索的大部分书籍都有预览。如果有预览,可以单击一个小的 About this book
link 将您带到发布信息。
我想单击第一个 link,如果有预览,请单击 "About this book." 我有以下内容,但我只收到 Error: object of type 'closure' is not subsettable
错误:
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',
'//*[@id="sidebar-atblink"]//parent::a')
link2 <- as.list(link2)
print(class(link2))
link2_about <- sapply(link2, function(ugh){
ugh$getElementAttribute('href')
})
} else {
print("nice going, dumbass")
}
}
或者我尝试 for
循环而不是 sapply
,我得到 Error: $ operator is invalid for atomic vectors
:
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")
}
}
如何才能成功点击到那一秒 link,具体取决于是否有预览?谢谢!
只需更新以下行。
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')
})
我有这个 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")
})
以上在 Google 搜索 return 中每 link 次点击(每页有 10 次)。单击进入后,我搜索的大部分书籍都有预览。如果有预览,可以单击一个小的 About this book
link 将您带到发布信息。
我想单击第一个 link,如果有预览,请单击 "About this book." 我有以下内容,但我只收到 Error: object of type 'closure' is not subsettable
错误:
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',
'//*[@id="sidebar-atblink"]//parent::a')
link2 <- as.list(link2)
print(class(link2))
link2_about <- sapply(link2, function(ugh){
ugh$getElementAttribute('href')
})
} else {
print("nice going, dumbass")
}
}
或者我尝试 for
循环而不是 sapply
,我得到 Error: $ operator is invalid for atomic vectors
:
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")
}
}
如何才能成功点击到那一秒 link,具体取决于是否有预览?谢谢!
只需更新以下行。
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')
})