英文:
Make click on list element using Rselenium
问题
我正在尝试在一个网页上进行一些网络抓取工作。网页看起来像这样:
如您所见,有一个列表,我需要点击第二个元素“Consulta por Cuenta Contrato”。我尝试了下面的代码,但它不起作用:
library(rvest)
library(tidyverse)
library(stringr)
library(stringi)
library(dplyr)
library(purrr)
library(wdman)
library(RSelenium)
library(xml2)
library(selectr)
library(httr)
library(jsonlite)
#Code
#start RSelenium
# 使用RSelenium启动selenium服务器上的Firefox
remDr <- rsDriver(
port = 4445L,
browser = "firefox"
)
#remDr$open()
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# 在Chrome上打开一个新标签
remDr$open()
# 导航到您的页面
remDr$navigate("https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso")
Sys.sleep(2)
webElem <- remDr$findElement(using = "id", "mat-radio-3")
webElem$clickElement()
我想要点击以获得这个结果:
使用RSelenium
可以实现吗?非常感谢。
英文:
I am trying to do some web scraping around a web page. The web page looks like this:
As you can see, there is a list and I need to make click on the second element Consulta por Cuenta Contrato
. I tried next code but is not working:
library(rvest)
library(tidyverse)
library(stringr)
library(stringi)
library(dplyr)
library(purrr)
library(wdman)
library(RSelenium)
library(xml2)
library(selectr)
library(httr)
library(jsonlite)
#Code
#start RSelenium
# using RSelenium to start firefox on the selenium server
remDr <- rsDriver(
port = 4445L,
browser = "firefox"
)
#remDr$open()
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# open a new Tab on Chrome
remDr$open()
#navigate to your page
remDr$navigate("https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso")
Sys.sleep(2)
webElem <- remDr$findElement(using = "id", "mat-radio-3")
webElem$clickElement()
I would like to make click so that I get this:
Is it possible to do this using RSelenium
? Many thanks.
答案1
得分: 1
代码中的问题是您没有正确选择单选按钮。如果检查网站的HTML,您会发现该项的ID不是您列出的mat-radio-3
。ID是mat-radio-3-input
。所以代码应该如下所示:
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# 在Chrome上打开一个新标签页
remDr$open()
# 转到您的页面
remDr$navigate("https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso")
Sys.sleep(2)
webElem <- remDr$findElement(using = "id", "mat-radio-3-input")
webElem$clickElement()
英文:
The issue with you code is that you aren't correctly selecting the radio button. If you inspect the HTML for the website, you'll see that the item does not have the id mat-radio-3
as you have listed. The id is mat-radio-3-input
. So the code should look like:
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# open a new Tab on Chrome
remDr$open()
#navigate to your page
remDr$navigate("https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso")
Sys.sleep(2)
webElem <- remDr$findElement(using = "id", "mat-radio-3-input")
webElem$clickElement()
答案2
得分: 0
看起来问题可能在于所需的元素被随机分配了id,要么是mat-radio-3
,要么是mat-radio-3-input
。
因此,另一种方法可能是通过部分类名匹配选择mat-radio-button
,然后选择第二个元素来调用clickElement()
方法。
webElem <- remDr$findElements(using = "class name", "mat-radio-button")
webElem[[2]]$clickElement()
英文:
It appears the issue at hand might be that the desired element is assigned an id of either mat-radio-3
or mat-radio-3-input
at random.
Therefore, an alternative approach might be to select by partial class name matching to mat-radio-button
and then select the second element from which to call the clickElement()
method.
webElem <- remDr$findElements(using = "class name", "mat-radio-button")
webElem[[2]]$clickElement()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论