• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / Javascript in Selenium Webdriver and Different usage

Javascript in Selenium Webdriver and Different usage

October 2, 2020 by Mukesh Otwani 12 Comments

Implement JavaScript in Selenium To execute JavaScript in our webdriver script we do not have to write the separate code we have one predefined interface available. JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor; Inside this Interface we have some predefined method called executeScript()- so whatever script you will pass as a String It will be executed by JavascriptExecutor Note- This is most common question in interview that how to type in Selenium without using sendKeys method Let us implement the same Program 1- How to type in Selenium without using sendKeys() method import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox { public static void main(String[] args) throws InterruptedException { // Open Firefox browser FirefoxDriver driver=new FirefoxDriver(); // Maximize the window driver.manage().window().maximize(); // Open applicatiion driver.get("enter your application URL"); // This will execute JavaScript in your script ((JavascriptExecutor)driver).executeScript("document.getElementById('some id').value='mukesh'"); } } Program 2- How to click in Selenium if button or radio button is disable package seleniumday1; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox { public static void main(String[] args) throws InterruptedException { // Open Firefox browser FirefoxDriver driver=new FirefoxDriver(); // Maximize the window driver.manage().window().maximize(); // Open applicatiion driver.get("enter your application URL"); // This will execute JavaScript in your script ((JavascriptExecutor)driver).executeScript("document.getElementById('enter your element id').click()"); } } Program 3- How to uncheck checkbox in Selenium if checkbox is disable package seleniumday1; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox { public static void main(String[] args) throws InterruptedException { // Open Firefox browser FirefoxDriver driver=new FirefoxDriver(); // Maximize the window driver.manage().window().maximize(); // Open applicatiion driver.get("enter your application URL"); // This will execute JavaScript in your script ((JavascriptExecutor)driver).executeScript("document.getElementById('enter element id').checked=false"); } } Please comment below if you are facing any issue. Thanks for visiting my blog. Keep in touch. Happy Testing

Hello, Welcome to Selenium Tutorials in this post we will see how we can introduce Javascript in Selenium Webdriver and what are the usage?

What is JavaScript

JavaScript is one of the programming languages of the Web.

All modern HTML pages are using JavaScript.

For more details, you can refer Wikipedia – JavaScript

 

Use of Javascript in Selenium Webdriver

We have used Java in our script and we implemented almost all features but some features we can’t handle or implement using Java so we need scripting language as well which can control server side or client side

Javascript in Selenium Webdriver

 

Implement JavaScript in Selenium Webdriver

To execute JavaScript in our webdriver script we do not have to write the separate code we have one predefined interface available.

JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor;

Inside this Interface we have some predefined method called executeScript()- so whatever script you will pass as a String  It will be executed by JavascriptExecutor.

 

 

Note- This is the most common question in the interview that how to type in Selenium without using the sendKeys method.

Let us implement Javascript in Selenium Webdriver

Program 1- How to type in Selenium without using sendKeys() method

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestFirefox {

public static void main(String[] args) throws InterruptedException {

// Open Firefox browser
FirefoxDriver driver=new FirefoxDriver();

// Maximize the window
driver.manage().window().maximize();

// Open applicatiion
driver.get("enter your application URL");

// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('some id').value='mukesh';");

}

}

 

Program 2- How to click in Selenium if button or radio button is disable

package seleniumday1;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestFirefox {

public static void main(String[] args) throws InterruptedException {

// Open Firefox browser
FirefoxDriver driver=new FirefoxDriver();

// Maximize the window
driver.manage().window().maximize();

// Open applicatiion
driver.get("enter your application URL");

// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('enter your element id').click();");

}

}

 

Program 3- How to uncheck checkbox in Selenium if checkbox is disable

package seleniumday1;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestFirefox {

public static void main(String[] args) throws InterruptedException {

// Open Firefox browser
FirefoxDriver driver=new FirefoxDriver();

// Maximize the window
driver.manage().window().maximize();

// Open applicatiion
driver.get("enter your application URL");

// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('enter element id').checked=false;");
}

}

 

 Please comment below if you are facing any issue. 

Thanks for visiting my blog. Keep in touch.

Happy Testing

Filed Under: Advance Selenium Tagged With: AutoIT File upload

Reader Interactions

Comments

  1. jaffar ali says

    July 17, 2019 at 1:32 AM

    ((JavascriptExecutor)driver).executeScript(“document.getElementById(‘some id’).value=’mukesh’;”);

    In the above code, what is the “document”? Can you please explain

    Thanks

    Reply
    • Mukesh Otwani says

      July 17, 2019 at 9:33 AM

      Hi Jaffar,

      document here refers object which represents your web page so in order to access any element in your HTML page, you always start with document object.

      Reply
  2. Gaurav Khurana says

    September 27, 2016 at 11:28 PM

    It would be great if you can share the application where the above example can be tested

    Could you please help me with the below 2 problems which are coming in searcging elements on the below site

    drv.get(“http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_checkbox_disabled2”);

    WebElement el = drv.findElement(By.xpath(“//button[@onclick=’myFunction()’]”));

    el.click();

    ((JavascriptExecutor)drv).executeScript(“document.getElementById(‘myCheck’).click();”);

    Problem 1 :- System is not able to find the element by xpath(even though it works in firepath)
    I have changed it to
    WebElement el = drv.findElement(By.cssSelector(“button[onclick*=’my’]”));
    this is also detectable via firepath but not by program

    Problem 2 :- there is an element with id = myCheck but that is also not able to detect while executing the above javascript

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:45 AM

      Hey Gaurav,

      The reason why your code is not working because it is inside frame so you can try the same html code in normal html file and try again.

      Reply
  3. Gaurav Khurana says

    September 24, 2016 at 7:24 PM

    hi

    Could you please share the situation where we may need to use javascript instead of selenium commands, apart from the interview question thing

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 11:14 AM

      Hi Gaurav,

      Below post will solve your queries http://learn-automation.com/click-in-selenium-webdriver-using-java-script/
      and http://learn-automation.com/invalidelementstateexception-in-selenium-webdriver/

      Reply
  4. Ashley says

    July 20, 2016 at 9:47 PM

    Thanks alot Mukesh for this useful tutorial. Please share me the answer of interview questions.

    Best,
    Ashley

    Reply
    • Mukesh Otwani says

      July 25, 2016 at 12:25 PM

      Hey Ashley,

      Kindly join mailing list to get Answers 🙂

      Reply
  5. vijay says

    June 12, 2016 at 5:03 AM

    Hi Mukesh

    its really helpful. Many thanks for your kind efforts

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:15 PM

      Hey Vijay,

      Most welcome keep visiting.

      Reply
  6. hanumanthappa says

    January 19, 2016 at 2:42 PM

    Hi MuKesh,
    Please share me interview question links

    Regards
    Hanuamthappa

    Reply
    • Mukesh Otwani says

      January 20, 2016 at 1:06 AM

      Here you go

      http://learn-automation.com/selenium-interview-questions-and-answers/

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?