简体   繁体   中英

How to execute a user defined JavaScript function using executeScript method of Selenium Java web driver

This is my code so far to execute function using Selenium:

JavascriptExecutor js=(JavascriptExecutor) driver;    
script="startDialog('arg1','arg2');";    
js.executeScript(script);    

Javascript function to be executed:

$(document).ready(function() {    
    var Btn = document.getElementById("btnid");  
    Btn.onclick = function() {  
    startDialog("arg1", "arg2");  
   };  
});

Unable to execute startDialog function using executeScript function of Selenium Java web driver.

Where is startDialog defined? The function you refer to isnt named and only calls startDialog.

Ofter the exception is correct. The function you are trying to call is undefined.

The script has to be the exact java script, not a function name. Try to assign the body of the function for script variable and then executing it. Like this -

script="$(document).ready(function() {" +     
"var Btn = document.getElementById('btnid');"+  
"Btn.onclick = function() {" +
"startDialog('arg1', 'arg2');};});"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM