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?
To access any Selenium WebDriver methods, first, we need to create a driver object using a WebDriver reference variable and then all the public methods will appear for that object. Thus, we can call Selenium WebDriver methods using an object reference variable.
Selenium is an open-source automation testing tool that supports a number of scripting languages like C#, Java, Perl, Ruby, JavaScript, etc.
a) executeScript This method executes JavaScript in the context of the currently selected window or frame in Selenium.
Try this:
WebDriver driver = new ChromeDriver();
((JavascriptExecutor)driver).executeScript("yourScript();");
WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor)
{
((JavascriptExecutor)driver).executeScript("yourScript();");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With