使用RSelenium包执行jQuery函数

时间:2015-09-03 16:46:15

标签: jquery r rselenium

我尝试使用import numpy as np matrix = np.matrix('1, 1, 1, 1; 1, 0, 0, 0; 0, 0, 1, 1') matrix = 1 1 1 1 1 0 0 0 0 1 0 1 0 0 1 1 软件包自动执行登录网站并对其执行某些过程的过程。我已经能够登录,点击这里和那里的按钮,但我坚持在页面上执行RSelenium功能。有一个下拉框,使用jQuery函数填充其中的数据。我不确定如何执行此功能。页面源(包括jQuery函数)如下:

jQuery

下拉列表的名称为 <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> ,我使用以下代码进行访问:

stuff

当我尝试执行以下命令时:

library("RSelenium")

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

我收到以下错误:

wxChooseStuff$clickElement()

我希望点击会自动填充下拉列表中的数据。

任何关于如何使用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 执行jQuery函数的指针都将非常感激。

即使我可以使用另一个包执行RSelenium函数,也没关系。我想执行此功能并单击元素。

PS - 我不是网络开发者,所以请原谅我,如果我问一个愚蠢的问题。

修改

我根据建议尝试了以下代码:

在此命令中,我只包含jQuery标记中的完整文字,将所有双引号(script)替换为单引号("

'

但我收到以下错误:

 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()

看起来仍然没有找到该元素。

2 个答案:

答案 0 :(得分:2)

如果您使用的是Chrome浏览器,请在RSelenium中右键单击要“点击”的元素,然后选择Inspect。进入开发人员控制台后,再次右键单击突出显示的元素,然后选择Copy/Copy Xpath。最后,在您的R代码中使用findElement(using="xpath", "xpath string you've copied")。根据我的经验,RSelenium以使用ID在页面上查找内容而臭名昭着,而XPath(再次,对我来说)更强大。

答案 1 :(得分:0)

我不知道你正在使用什么驱动程序,但使用PHP的chrome驱动程序,你可以这样做:

$javascript = array('script' => 'myfunction();', 'args' => array());
$var = $this->execute($javascript);