r语言 - 使用Rselenium提取href标签



我正在尝试使用Rselenium获取多个国家的apple商店地址。

library(RSelenium)
library(tidyverse)
library(netstat)
# start the server
rs_driver_object <- rsDriver(browser = "chrome",
chromever = "100.0.4896.60",
verbose = F,
port = free_port())
# create a client object
remDr <- rs_driver_object$client
# maximise window size
remDr$maxWindowSize()
# navigate to the website
remDr$navigate("https://www.apple.com/uk/retail/storelist/")
# click on search bar
search_box <- remDr$findElement(using = "id", "dropdown")
country_name <- "United States" # for a single country. I can loop over multiple countries
# in the search box, pass on the country name and hit enter
search_box$sendKeysToElement(list(country_name, key = "enter"))
search_box$clickElement() # I am not sure if I need to click but I am doing anyway 

页面现在显示了每个商店的位置。每个商店都有一个超链接,它会把我带到商店网站,我要提取的完整地址是

然而,我被困在我如何在最后一步点击个人商店地址。我想我将获得特定页面中所有商店的href

store_address <- remDr$findElement(using = 'class', 'store-address')
store_address$getElementAttribute('href')

但是它返回给我一个空列表。我该怎么走?

在获得包含商店列表的页面后,

link = remDr$getPageSource()[[1]] %>%
read_html() %>% html_nodes('.state') %>% html_nodes('a') %>% html_attr('href') %>% paste0('https://www.apple.com', .)
[1] "https://www.apple.com/retail/thesummit/"              "https://www.apple.com/retail/bridgestreet/"          
[3] "https://www.apple.com/retail/anchorage5thavenuemall/" "https://www.apple.com/retail/chandlerfashioncenter/" 
[5] "https://www.apple.com/retail/santanvillage/"          "https://www.apple.com/retail/arrowhead/"  

最新更新