
# package
http://ropensci.github.io/RSelenium/index.html

```{r}
library(RSelenium)
library(tidyverse)
```

Открываем браузер
```{r}
pJS <- wdman::phantomjs(port = 4568L)

Sys.sleep(5) # give the binary a moment
```

Подключаемся к браузеру и открываем его
```{r}
remDr <- remoteDriver(browserName = 'phantomjs', port = 4568L)
remDr$open()
```

Открываем сайт
```{r}
remDr$navigate("https://ria.ru/abitura_rus/20180417/1518196987.html")

remDr$screenshot(display = T)
```

```{r}
el = remDr$findElement("xpath", '//input[@placeholder="Предметная область"]')
el$clickElement()

remDr$screenshot(display = T)
```

```{r}
el = remDr$findElements("xpath", '//div[@class="rating__filters1-select-opt"]//li')

x = el[[1]]
x$getElementText()

subjects = sapply(el, function(x){ x$getElementText() }) %>% 
  unlist()
```

```{r}

element_xpath = '//div[@class="rating__filters1-select-opt"]//li[contains(text(), "ОБРАЗОВАНИЕ И ПЕДАГОГИЧЕСКИЕ НАУКИ")]'
el = remDr$findElement("xpath", element_xpath)
  
el$clickElement()

remDr$screenshot(display = T)

```

```{r}
remDr$navigate("https://ria.ru/abitura_rus/20180417/1518196987.html")

for(i in 1:length(subjects)){
  el = remDr$findElement("xpath", '//input[@placeholder="Предметная область"]')
  el$clickElement()
  
  element_xpath = str_c('//div[@class="rating__filters1-select-opt"]//li[contains(text(), "', subjects[i], '")]')
  el = remDr$findElement("xpath", element_xpath)
  
  el$clickElement()
}

remDr$screenshot(display = T)
```

```{r}
el = remDr$findElement("xpath", '//input[@placeholder="Специальность"]')
el$clickElement()

remDr$screenshot(display = T)

el = remDr$findElements("xpath", '//div[@ng-show="showDropDown[2]"]//li')

x = el[[1]]
x$getElementText()

prof = sapply(el, function(x){ x$getElementText() }) %>% 
  unlist()

for(i in 1:length(prof)){
  el = remDr$findElement("xpath", '//input[@placeholder="Специальность"]')
  el$clickElement()
  
  element_xpath = str_c('//div[@class="rating__filters1-select-opt"]//li[contains(text(), "', prof[i], '")]')
  el = remDr$findElement("xpath", element_xpath)
  
  el$clickElement()
}

remDr$screenshot(display = T)
```

```{r}
el = remDr$findElements("xpath", '//div[@class="rating__search-university-title ng-binding"]')

for(i in 1:length(el)){
  uni = el[[i]]
  
  uni$clickElement()
  
  
}
```

```{r}
remDr$close
pJS$stop()
```
