How to execute JavaScript in Robot Framework

寵の児 提交于 2019-12-07 12:42:03

问题


Below is my code. When I run this, it shows a WebDriverException. How do I execute JavaScript code in Robot Framework?

This, return $(arguments[0]).data('${ToolTip}').options.title code is doing well in -java Selenium web driver.

Mouse Over    ${CreateTask}
    Execute JavaScript    return $(arguments[0]).data('${ToolTip}').options.title

回答1:


From http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Execute%20Javascript:

Note that, by default, the code will be executed in the context of the Selenium object itself, so this will refer to the Selenium object. Use window to refer to the window of your application, e.g. window.document.getElementById('foo').

So

Mouse Over    ${CreateTask}
    Execute JavaScript    return window.$(arguments[0]).data('${ToolTip}').options.title

Assuming there is some library (jQuery most probably) that actually understands the $ shorthand.




回答2:


arguments[0] normally refers to arguments passed to your script.

Selenium2Library's Execute JavaScript calls webdriver.execute_javascript and does not pass any arguments to it. arguments[0] is therefore undefined.

See Is there a way to provide arguments to "Execute JavaScript" in Robot Framework? for a workaround.



来源:https://stackoverflow.com/questions/21822386/how-to-execute-javascript-in-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!