使getElementText通过循环

时间:2019-04-25 14:13:48

标签: r loops rselenium

我使用RSelenium软件包进行网页抓取

我的代码:

library(RSelenium)
driver <- rsDriver(port=4444L,browser="chrome")
remote_driver <- driver$client
remote_driver$open()
remote_driver$navigate("https://www.amazon.in/b/?_encoding=UTF8&node=1389401031&ref_=sv_top_elec_mega_1")
one<-remote_driver$findElement(using = "class", value="crwTitle")
k<- one$getElementText()

它可以工作,但是只返回一条记录,如下所示

k的输出:

[1] "Samsung Galaxy M30 (Gradation Blue, 4+64 GB)"

我试图创建一个循环

gettx <- lapply(one,function(one) {
  one$getElementText()
})

但是我遇到了这个错误

Error in one$getElementText : object of type 'closure' is not subsettable

我期望的是


Samsung Galaxy M30 (Gradation Blue, 4+64 GB)
Samsung Galaxy M20 (Ocean Blue, 4+64GB)
Redmi 6A (Black, 2GB RAM, 16GB Storage)

...........etc

我的意思是我需要返回所有具有相同类的元素。

有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

您可以这样做:

<table id="myTable">
  <tr>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
  </tr>
  <tr>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
    <td><button type="button"></button></td>
  </tr>
</table>

请注意,remDr$navigate("https://www.amazon.in/b/?_encoding= UTF8&node=1389401031&ref_=sv_top_elec_mega_1") elems <- remDr$findElements(using = "class", value="crwTitle") lapply(elems, function(elem) { elem$getElementText() }) 仅查找单个元素,而remDr$findElement()仅查找元素列表。