How to call a JavaScript function from within Selenium?

javascript, selenium

Solution

WebDriver driver = new AnyDriverYouWant();

if (driver instanceof JavascriptExecutor)

{

((JavascriptExecutor)driver).executeScript("yourScript();");

}

Problem

I need to call a JavaScript function from Selenium WebDriver in Firefox. I use this command in Firebug's Command Editor to invoke a file upload application after logged into my website: ``` infoPanel.applicationManager.changeApp('FileUploader', {action: 'new'}) ``` Is there a way to execute this from Selenium?

Original source