Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

r - How to select dropdown box using Rselenium?

I am interested in the stats in the English Premier League. So, I try to get data from this official site https://www.premierleague.com/stats/top/players/total_pass

I am using R and RSelenium package.

library(rvest)
library(httr)
library(RSelenium)

remDr <- remoteDriver(port = 4445L)
remDr$open()
remDr$navigate('https://www.premierleague.com/stats/top/players/total_pass')
getsource <-remDr$getPageSource()
name<- read_html(getsource[[1]]) %>% html_nodes("strong") %>% html_text()

But I got the some problems. There are some categories of data such as seasons, positions, clubs and so on.

So, I thought that I can get data based on these categories. But I did not know how to select specific things in the dropdown box using Rselenium in this site.

I thought that filenElement and clickElement are useful functions for this. However, I do not know how I should handle these functions to select specific conditions such as 2016/17 season and Goalkeeper position.

Please give me an advice for this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the following code I was able to get the browser to select the 2014/15 season. You will need to inspect the contents of the various drop-downs and expand this as required.

rD <- rsDriver(port=4444L,browser="chrome")
remDr <- rD$client

#navigate to main page
remDr$navigate('https://www.premierleague.com/stats/top/players/total_pass')

#find 'filter by season' box and click it
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-dropdown-block='FOOTBALL_COMPSEASON']")
webElem$clickElement()

#find 2014/15 season and click it
webElem1 <- remDr$findElement(using = 'xpath', value = "//*[@data-option-name='2014/15']")
webElem1$clickElement()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...