首页 > 解决方案 > 在 Rselenium 中处理弹出的身份验证窗口

问题描述

这个社区中的任何人都知道在使用 RSelenium 进行网络抓取时如何在弹出窗口中处理(如何输入用户名和密码)?我正在尝试登录以下数据门户并自动进行批量下载。网页:https://land.copernicus.vgt.vito.be/PDF/portal/Application.html#Browse;Root=512260;Collection=1000084;Time=NORMAL,NORMAL,-1,,,-1,,

标签: rrseleniumdocker-selenium

解决方案


通常,我会从检查网页中复制 xpath 并使用它来查找元素 - 就像我在这里所做的那样:

library(RSelenium)
library(netstat)

rD <- rsDriver(port = free_port(), browser = 'chrome', 
               chromever = "96.0.4664.45",  
               verbose = F)

remDr <- rD[["client"]]

# navigate - I used the url from your comment 
remDr$navigate("https://land.copernicus.vgt.vito.be/PDF/portal/Application.html#Home")

# find the login element
l <- remDr$findElement(using = "id", "login")
# there it is
l$highlightElement(); l$clickElement() # pops up the box to enter credentials 
  

# find the username
l1 <- remDr$findElement(using = "xpath", "/html/body/div[13]/div/table/tbody/tr[2]/td[2]/div/div/form/table/tbody/tr[1]/td[2]/input")
# there it is 
l1$highlightElement()
# sendkeys for username
l1$sendKeysToElement(list("username")) # replace with your username 

# find the password
l2 <- remDr$findElement(using = "xpath", "/html/body/div[13]/div/table/tbody/tr[2]/td[2]/div/div/form/table/tbody/tr[2]/td[2]/input")
# there it is 
l2$highlightElement()
# sendkeys to password
l2$sendKeysToElement(list("password"))

# login 
l3 <- remDr$findElement(using = "xpath", "/html/body/div[13]/div/table/tbody/tr[2]/td[2]/div/div/form/table/tbody/tr[5]/td/button[1]")
# there it is
l3$highlightElement(); l3$clickElement()

推荐阅读