点击列表元素使用 Rselenium

huangapple go评论74阅读模式
英文:

Make click on list element using Rselenium

问题

我正在尝试在一个网页上进行一些网络抓取工作。网页看起来像这样:

点击列表元素使用 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

使用RSelenium可以实现吗?非常感谢。

英文:

I am trying to do some web scraping around a web page. The web page looks like this:

点击列表元素使用 Rselenium

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 &lt;- rsDriver(
  port = 4445L,
  browser = &quot;firefox&quot;
)
#remDr$open()
remDr &lt;- remoteDriver(port = 4445L,browser = &quot;firefox&quot;)
# open a new Tab on Chrome
remDr$open()
#navigate to your page
remDr$navigate(&quot;https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso&quot;)
Sys.sleep(2)
webElem &lt;- remDr$findElement(using = &quot;id&quot;, &quot;mat-radio-3&quot;)
webElem$clickElement()

I would like to make click so that I get this:

点击列表元素使用 Rselenium

Is it possible to do this using RSelenium? Many thanks.

答案1

得分: 1

代码中的问题是您没有正确选择单选按钮。如果检查网站的HTML,您会发现该项的ID不是您列出的mat-radio-3。ID是mat-radio-3-input。所以代码应该如下所示:

remDr &lt;- remoteDriver(port = 4445L,browser = &quot;firefox&quot;)
# 在Chrome上打开一个新标签页
remDr$open()
# 转到您的页面
remDr$navigate(&quot;https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso&quot;)
Sys.sleep(2)
webElem &lt;- remDr$findElement(using = &quot;id&quot;, &quot;mat-radio-3-input&quot;)
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 &lt;- remoteDriver(port = 4445L,browser = &quot;firefox&quot;)
# open a new Tab on Chrome
remDr$open()
#navigate to your page
remDr$navigate(&quot;https://nodejs.eeq.com.ec/consultafacturas/proceso/formulario-ingreso&quot;)
Sys.sleep(2)
webElem &lt;- remDr$findElement(using = &quot;id&quot;, &quot;mat-radio-3-input&quot;)
webElem$clickElement()

答案2

得分: 0

看起来问题可能在于所需的元素被随机分配了id,要么是mat-radio-3,要么是mat-radio-3-input

因此,另一种方法可能是通过部分类名匹配选择mat-radio-button,然后选择第二个元素来调用clickElement()方法。

webElem &lt;- 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 &lt;- remDr$findElements(using = &quot;class name&quot;, &quot;mat-radio-button&quot;)
webElem[[2]]$clickElement()

huangapple
  • 本文由 发表于 2023年6月12日 05:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定