Francisco Galindo
Francisco Galindo

Reputation: 1

Click a button in a web using R

I want to autofill a form in a webpage. I am using RSelenium, and I get to complete all the form, but the "preferred language" field.

The webpage is: https://ontrack.sgs.com/es-ES/registration-form/

I also cannot solve the captcha, but the best wqay for that I figure that will be using an API.

I have tried to locate de button to click and get the pop up appear to select, but I haven't been able to do that.

I have tried this combinations and many more, but nothing seems to work:

library(RSelenium)

#browser parameters

rD <- rsDriver(port=4571L, browser = "chrome", chromever = "75.0.3770.90") # runs a chrome browser, wait for necessary files to download
remDrv <- rD$client
remDrv$open(silent = TRUE)
url<-paste("https://ontrack.sgs.com/es-ES/registration-form/",sep="")

#### Updating form

webElem <- remDr$findElement(using = 'class', "button")
webElem <- remDrv$findElement(using = "xpath", "//input[@class='button']")
# webElem <- remDr$findElement(using = 'css selector', ".launchentitylookup")
# webElem$clickElement()

webElem <- remDrv$findElement(using = "xpath", "//input[@name = 'ctl00$ctl00$ContentContainer$MainContent$EntityControls$EntityFormControl$EntityFormControl_EntityFormView$sgs_portallanguage_name' ]") 
webElem$sendKeysToElement(list("Español"))
webElem <- remDrv$findElement(using = "xpath", "//input[@name = 'ctl00$ctl00$ContentContainer$MainContent$EntityControls$EntityFormControl$EntityFormControl_EntityFormView$sgs_portallanguage' ]") 
webElem$sendKeysToElement(list("Español"))
webElem <- remDrv$findElement(using = "xpath", "//input[@name = 'ctl00$ctl00$ContentContainer$MainContent$EntityControls$EntityFormControl$EntityFormControl_EntityFormView$sgs_portallanguage_entityname' ]") 
webElem$sendKeysToElement(list("Español"))

I expect to be able to select a language in the form, Spanish in fact.

Apart from that, being able to solve or skip the captcha.

Upvotes: 0

Views: 1418

Answers (1)

Abhishek Arora
Abhishek Arora

Reputation: 121

I must be very late but here is an answer to the query. Note that I am not including the captcha bit here because that isn't a part of the main question.

    library(RSelenium)

#browser parameters

rD <- rsDriver(port=4571L, browser = "chrome", chromever = "87.0.4280.20") # runs a chrome browser, wait for necessary files to download
remDrv <- rD$client
remDrv$open(silent = TRUE)
url<-paste("https://ontrack.sgs.com/es-ES/registration-form/",sep="")

remDrv$navigate(url)
#### Updating form

##open language search
remDrv$findElement(using = "css", "#sgs_portallanguage_entityname+ .input-group-btn .launchentitylookup")$clickElement()
Sys.sleep(3) ## wait for languages to load

#select lanaguage - should match the ones in the table
language<-'Español'

###language path
lang_path<-paste0("//*[contains(concat( ' ', @class, ' ' ), concat( ' ' , 'table-hover', ' ' ))]//*[contains(concat( ' ', @data-value, ' ' ), concat( ' ' , '",language,"', ' ' ))]")  


##selec language - Spanish
remDrv$findElement(using="xpath",lang)$clickElement()


##Press Select button
select_path<-"//*[(@id = 'sgs_portallanguage_lookupmodal')]//*[@class='primary btn btn-primary']"
remDrv$findElement(using="xpath",select_path)$clickElement()

Probably late but I hope it helps someone. The key was to cleverly construct xpaths.

Upvotes: 1

Related Questions