4

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

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting same issue.
Can you share us the page in question or at least a simplified version? Are you sure that your syntax is correct? It looks weird. Are you able to execute any Javascript at all? You are not storing the output of Execute Javascript to any variable, is that intentional? Does ${ToolTip} have proper value? Where does it come from?
yeah i am able to execute Execute JavaScript document.getElementsByName("create_button")[0].click() and ${ToolTip} xpath=//tr[2]/td[2]/span. Webdriver code is working correclty JavascriptExecutor executor = (JavascriptExecutor) driver; String toolString = (String) executor.executeScript( "return $(arguments[0]).data('//tr[2]/td[2]/span').options.title;", "//tr[2]/td[2]/span");
${title}= Execute Javascript return $('//tr[2]/td[2]/span').data('//tr[2]/td[2]/span').options.title;
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.

Comments

1

Use any one method from below options

   Execute Javascript    document.getElementById('authUser').value='admin'
 
   ${ele}    Get WebElement    id=clearPass
   Execute Javascript    arguments[0].value='pass';     ARGUMENTS    ${ele}
    
   Execute Javascript    alert(arguments[0]);    ARGUMENTS    123 
   
   Execute Javascript    document.evaluate("//*[text()='Patients']",document.body,null,9,null).singleNodeValue.click();
    
   
   Execute Javascript    document.querySelector('[data-bind="click: changePassword"]').click();
   
   
   ${ele}    Get WebElement    //*[text()='Logout']
   Execute Javascript    arguments[0].click();     ARGUMENTS    ${ele} 
   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.