Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute jQuery function using RSelenium package

I'm trying to automate a process of logging on to a website and performing some process on it, using the RSelenium package. I have been able to login, click on buttons here and there, but I am stuck at executing a jQuery function on the page. There's a dropdown box which populates data in it using a jQuery function. I'm not sure how to execute this function. The page source (including the jQuery function) is as follows:

 <input disabled="disabled" id="stuff" name="stuff" style="width:100%" type="text" /><script>
    jQuery(function(){jQuery("#stuff").kendoDropDownList({"change":disableNext,"dataSource":{"transport":{"read":{"url":"/StuffInfo/GetStuff","data":filterStuff},"prefix":""},"serverFiltering":true,"filter":[],"schema":{"errors":"Errors"}},"autoBind":false,"optionLabel":"Select court...","cascadeFrom":"state"});});
</script>
            <script>

The name of the dropdown is stuff and I'm using the following code to access it:

library("RSelenium")

startServer()
mybrowser <- remoteDriver()
mybrowser$open()
mybrowser$navigate("<URL>")
wxChooseStuff <- mybrowser$findElement(using='id',"stuff")

when I try to execute the following command:

wxChooseStuff$clickElement()

I get the following error:

Error:   Summary: ElementNotVisible
     Detail: An element command could not be completed because the element is not visible on the page.
     class: org.openqa.selenium.ElementNotVisibleException

I was hoping that the click would auto-populate data in the dropdown.

Any pointers on how to execute the jQuery function using RSelenium would be much appreciated.

Even if I can execute the jQuery function using another package, that will be fine. I would like to just execute this function and click on the element.

PS - I'm not a web developer, so pardon me if I'm asking a stupid question.

EDIT:

I tried the following code as per suggestion:

In this command, I just include the complete text enclosed in the script tag, replacing all the double quotes (") with single quotes (')

 mybrowser$executeScript(script = "jQuery(function(){jQuery('#stuff').kendoDropDownList({'change':disableNext,'dataSource':{'transport':{'read':{'url':'/StuffInfo/GetStuff','data':filterStuff},'prefix':''},'serverFiltering':true,'filter':[],'schema':{'errors':'Errors'}},'autoBind':false,'optionLabel':'Select court...','cascadeFrom':'state'});});")

wxChooseStuff <- mybrowser$findElement(using='id',"stuff")
mybrowser$executeScript(script = "arguments[0].hidden = false;", 
                        args = list(wxChooseStuff))
wxChooseStuff$clickElement()

but I received the following error:

Error:   Summary: ElementNotVisible
     Detail: An element command could not be completed because the element is not visible on the page.
     class: org.openqa.selenium.ElementNotVisibleException

Looks like the element is still not to be found.

like image 591
Patthebug Avatar asked Sep 03 '15 16:09

Patthebug


1 Answers

If you're using Chrome browser, right-click on the element you want to "click" in RSelenium, and choose Inspect. Once in the developer console, right-click again on the highlighted element and choose Copy/Copy Xpath. Finally, in your R code use findElement(using="xpath", "xpath string you've copied"). In my experience RSelenium has notoriously issues with finding things on the page using ID, whereas XPath is (again, for me) way more robust.

like image 60
PSzczesny Avatar answered Sep 18 '22 12:09

PSzczesny