1

I've created a script as the I want to use in another Selenium WebDriver script:

$function() {
    $("pane1").hide(300);
});

I'm trying to figure out a way to call this script in my Selenium java code.

2
  • Please specify more detail? What you want to call? and for what? Commented Feb 14, 2015 at 3:42
  • I want to call the jquery as seen above in a webdriver script I have created. I want the script to be called and hide the element pane1. Commented Feb 14, 2015 at 11:29

1 Answer 1

3

Calling jQuery functions from Selenium is done exactly the same way as calling any other. However, there are two issues with your code:

  1. You have $function, where you probably mean $(function. If you tried to execute the code in your question as-is you certainly got an error because of this.

  2. Ok, let's say you fix that problem. Now you have a $(function () {...}) call. This is not harmful but it is pointless as you are essentially saying "execute this function when the page has finished its initial load". If you use Selenium the way it is usually used, it won't return control to you until the page has finished its initial load, so there is no reason to wait for the page load.

So:

((JavascriptExecutor) driver).executeScript("$('pane1').hide(300);");
Sign up to request clarification or add additional context in comments.

3 Comments

What would I do if it is a very large script with many functions I wish to call?
Pass the large script to executeScript. I've passed fairly large scripts to it. I don't know of any hard limit to the script size. However, I'm pretty sure it was not design to execute super large code bases so I would not be surprised if beyond a certain size Selenium would choke, even if there is no hardcoded limit.
An example of passing the large script would be greatly appreciated if you could give one?thanks

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.