Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
View Categories
  • Home
  • Tutorials
  • Selenium Automation Testing
  • How to handle basic functionality using JavaScriptExecutor?

How to handle basic functionality using JavaScriptExecutor?

How to handle basic functionality

using JavaScriptExecutor?

We can use JavaScriptExecutor in Selenium to interact with elements such as text boxes, checkboxes, radio buttons, dropdowns, and buttons, especially when standard Selenium methods (.click(), .sendKeys(), etc.) do not work due to JavaScript frameworks, hidden elements, or overlays.

Below are JavaScriptExecutor code examples for each type in Java:

1. Text Box: Set Value

📄
filename.js
WebElement textBox = driver.findElement(By.id("username"));

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].value='VinothQA';", textBox);

2. Checkbox: Check/Uncheck

Check the Checkbox

📄
filename.js
WebElement checkbox = driver.findElement(By.id("acceptTerms"));

js.executeScript("arguments[0].checked=true;", checkbox);

Uncheck the Checkbox

📄
filename.js
js.executeScript("arguments[0].checked=false;", checkbox);

Or Click (simulates user click via JS)

📄
filename.js
js.executeScript("arguments[0].click();", checkbox);

3. Radio Button: Select

📄
filename.js
WebElement radioBtn = driver.findElement(By.id("genderMale"));

js.executeScript("arguments[0].checked=true;", radioBtn);

// Or to simulate a user click:

js.executeScript("arguments[0].click();", radioBtn);

4. Dropdown (Select): Set Value

For a simple <select> element:

📄
filename.js
WebElement dropdown = driver.findElement(By.id("country"));

js.executeScript("arguments[0].value='IN';", dropdown); // Set value attribute

If you want to trigger events (like change), do:

📄
filename.js
js.executeScript("arguments[0].value='IN'; arguments[0].dispatchEvent(new Event('change'));", dropdown);

For custom dropdowns:
Click to open and select an option using JS:

📄
filename.js
// Example: click the dropdown

js.executeScript("arguments[0].click();", dropdown);

// Then click the desired option (locate it first)

WebElement option = driver.findElement(By.xpath("//li[text()='India']"));

js.executeScript("arguments[0].click();", option);

5. Button: Click

📄
filename.js
WebElement button = driver.findElement(By.id("submitBtn"));

js.executeScript("arguments[0].click();", button);

Summary Table

ElementJavaScriptExecutor Example
Text boxarguments[0].value=’text’;
Checkboxarguments[0].checked=true; or .click();
Radioarguments[0].checked=true; or .click();
Dropdownarguments[0].value=’value’; + dispatchEvent(new Event(‘change’));
Buttonarguments[0].click();

Tips:

  • Always pass the element as an argument (see arguments[0]).
  • For dropdowns, ensure you match the correct value or text.

Use dispatchEvent(new Event(‘change’)) to trigger change handlers after setting values programmatically.

Selenium Automation Testing
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
How to handle scroll function using JavaScriptExecutor?How to handle advanced functionality using JavaScriptExecutor?
© 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105