20

我正在尝试使用该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将不胜感激。

即使我可以jQuery使用另一个包执行该功能,也可以。我只想执行此功能并单击元素。

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

编辑:

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

在此命令中,我只包含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

看起来仍然找不到该元素。

4

2 回答 2

2

如果您使用的是 Chrome 浏览器,请在 RSelenium 中右键单击要“单击”的元素,然后选择Inspect. 进入开发者控制台后,再次右键单击突出显示的元素并选择Copy/Copy Xpath. 最后,在您的 R 代码中使用findElement(using="xpath", "xpath string you've copied"). 以我的经验,RSelenium 在使用 ID 在页面上查找内容方面存在臭名昭著的问题,而 XPath(再次对我而言)更加健壮。

于 2018-03-16T17:48:22.597 回答
0

我不知道您使用的是什么驱动程序,但是使用 PHP 的 chrome 驱动程序可以执行以下操作:

$javascript = array('script' => 'myfunction();', 'args' => array());
$var = $this->execute($javascript);
于 2017-02-22T22:52:09.497 回答