Java script function execution via web driver

254 views
Skip to first unread message

Kaustubh Joshi

unread,
May 15, 2014, 6:04:52 AM5/15/14
to seleniu...@googlegroups.com


Hi all,

HTML structure :-
a onclick="submitNewForm('All','Complete')">Save</a

Problem :- I want to simulate the onclick event as mentioned in above HTML structure..
I've written helper class as
public Object executeScript(String s, Object... args) {
        return ((JavascriptExecutor) driver).executeScript(s, args);
    }
   
    now when I am calling it like
   
    String onClickElement = "submitNewForm('All','Complete')"
     
     executeScript(onClickElement,'');
     
      the code is not triggering the onclick event.
     Can anyone suggest me where I need to make code fix so that I can trigger onclick event mentioned in html structure?Pls advise
     
     Please let me know if you've any questions.
     
    Awaiting the reply.

Thanks
K


David

unread,
May 15, 2014, 4:11:16 PM5/15/14
to seleniu...@googlegroups.com
Why not just click the link with Selenium rather than manually invoke the javascript method?

Another option is to invoke the click event through javascript instead of using Selenium click() method or calling the javacript function directly. For click event simulation, find the (mouse click) section/link under this post of mine:

Kaustubh Joshi

unread,
May 16, 2014, 9:08:30 AM5/16/14
to seleniu...@googlegroups.com
Thanks David.I did same and it's working for me.But still I'm curious to know where my manually invoking javascript method is getting wrong...hence asked all expert like you where I need to make code fix..because custom method I've written does not seem to have a problem when looking at..but I may be wrong...pls advise.

Thanks
K


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c6b7e13e-7b50-48d1-9d4f-e8e6a46682b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David

unread,
May 16, 2014, 2:17:27 PM5/16/14
to seleniu...@googlegroups.com
It might help if you figure out or show us what that javascript function actually does in case that makes any difference. I did notice that your executeScript wrapper function takes varargs but you don't pass any real args to it, just empty string, not sure if that matters.

You can also add another function call or other javascript code to see that the script is being executed. if it just doesn't execute your submitNewForm() function, then something is weird about it.

String onClickElement = "submitNewForm('All','Complete'); someOtherJsTestDebugFunction();"

or

String onClickElement = "someOtherJsTestDebugFunction(); submitNewForm('All','Complete');"

where the other function can be something that brings up an alert, or changes the style or text of an element like document.getElementById('some id').style = "font-weight: bold;" 

Datta More

unread,
May 15, 2014, 11:56:18 AM5/15/14
to seleniu...@googlegroups.com

Can you try Actions?

Refer examples from http://www.guru99.com/selenium-tutorial.html

May be this will help.

Thanks,
Datta

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Kaustubh Joshi

unread,
May 17, 2014, 5:33:36 AM5/17/14
to seleniu...@googlegroups.com
Thanks Datta, I'vr already written custom method using Actions and it's working but posted over here for why it's not working with JavascriptExecutor.......


Reply all
Reply to author
Forward
0 new messages