使用RSelenium包执行jQuery函数



我正在尝试自动化登录到一个网站的过程,并在它上执行一些过程,使用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

我希望点击会自动在下拉菜单中填充数据。

任何关于如何使用RSelenium执行jQuery函数的指针将不胜感激。

即使我可以使用另一个包执行jQuery函数,也没关系。我只想执行这个函数并单击元素。

PS -我不是一个web开发人员,所以如果我问了一个愚蠢的问题,请原谅我。

编辑:

我按照建议尝试了以下代码:

在这个命令中,我只包括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()

但是我收到了以下错误:

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

似乎仍然没有找到该元素。

如果你使用Chrome浏览器,右键单击你想在RSelenium中"点击"的元素,并选择Inspect。进入开发人员控制台后,再次右键单击突出显示的元素并选择Copy/Copy Xpath。最后,在R代码中使用findElement(using="xpath", "xpath string you've copied")。根据我的经验,RSelenium在使用ID查找页面上的内容时出了名的问题,而XPath(对我来说)要健壮得多。

我不知道你使用的是什么驱动程序,但是对于PHP的chrome驱动程序,你这样做:

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

最新更新