Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  • Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  • Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  • Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  • Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Images
  • Handling Mouse Events
  • Handling Keyboard Events
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
  • Types of wait statement in selenium
  • How to take screenshots?
  • How to upload File in Selenium?
  • How to handle scroll function using JavaScriptExecutor?
  • How to handle basic functionality using JavaScriptExecutor?
  • How to handle advanced functionality using JavaScriptExecutor?
  • How to automate CAPTCHA?
  • How to handle cookies in selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • How to handle advanced functionality using JavaScriptExecutor?

How to handle advanced functionality using JavaScriptExecutor?

How to handle advanced functionality using

JavaScriptExecutor?

1. Advanced Examples for Custom UI Frameworks (React, Angular, etc.)

Modern frameworks often use custom controls that don’t behave like classic HTML elements. Here’s how you can use JavaScriptExecutor to handle tricky situations:

A. Custom Checkbox (React/Angular Material, etc.)

Challenge:
The actual <input type=”checkbox”> may be hidden and clicking it doesn’t work. The visible part is often a styled <div>.

Solution:
Trigger the React/Angular click event on the visible element.

// Locate the visible container or label of the custom checkbox

WebElement customCheckbox = driver.findElement(By.cssSelector(".custom-checkbox")); // adjust selector

JavascriptExecutor js = (JavascriptExecutor) driver;

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

This triggers the underlying event handler, updating the UI and state.

B. Custom Dropdown (React-Select, Angular Material, etc.)

Challenge:
No native <select>—options are rendered as a list on click.

Solution:

  • Open the dropdown using JS.
  • Click the desired option using JS.

// Open the dropdown

WebElement dropdown = driver.findElement(By.cssSelector(".custom-dropdown"));

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

// Wait for options to be visible (add an explicit wait if needed)

WebElement option = driver.findElement(By.xpath("//div[text()='Desired Option']"));

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

This selects the desired value even when keyboard or standard Selenium doesn’t work.

C. React/Angular Input Field with Two-way Binding

Challenge:
Typing directly may not update the framework’s model.

Solution:
Set value, then dispatch input and change events to trigger reactivity.

WebElement input = driver.findElement(By.id("react-input"));

String value = "Hello Framework!";

js.executeScript(

    "arguments[0].value=arguments[1];" +

    "arguments[0].dispatchEvent(new Event('input', { bubbles: true }));" +

    "arguments[0].dispatchEvent(new Event('change', { bubbles: true }));",

    input, value

);

This ensures the framework recognizes the input change.

2. How to Read Values Using JavaScriptExecutor

To retrieve (return) values using JS:

A. Get Value of an Input or Text Box

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

JavascriptExecutor js = (JavascriptExecutor) driver;

String value = (String) js.executeScript("return arguments[0].value;", textBox);

System.out.println("Text box value: " + value);

B. Get Text Content of Any Element

WebElement element = driver.findElement(By.id("customLabel"));

String text = (String) js.executeScript("return arguments[0].textContent;", element);

System.out.println("Element text: " + text);

3. Error Handling for JavaScript Execution

It’s good practice to catch exceptions and print meaningful messages, especially when working with dynamic UIs.

Example:

try {

    WebElement element = driver.findElement(By.id("someElement"));

    JavascriptExecutor js = (JavascriptExecutor) driver;

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

    System.out.println("Clicked element using JS.");

} catch (org.openqa.selenium.JavascriptException je) {

    System.err.println("JavaScript execution failed: " + je.getMessage());

} catch (org.openqa.selenium.NoSuchElementException ne) {

    System.err.println("Element not found: " + ne.getMessage());

} catch (Exception e) {

    System.err.println("Other error: " + e.getMessage());

}

  • Use JavascriptException for JS-related issues.
  • Always log the error or take a screenshot on failure for debugging.

Summary Table

ScenarioApproach/Example
React/Angular Checkboxjs.executeScript(“arguments[0].click();”, customCheckbox);
Custom DropdownOpen dropdown & select: js.executeScript(“arguments[0].click();”, dropdown/option);
Two-way Binding InputSet value & fire events: js.executeScript(“arguments[0].value=…; arguments[0].dispatchEvent…”);
Read value with JSjs.executeScript(“return arguments[0].value;”, element);
Read text contentjs.executeScript(“return arguments[0].textContent;”, element);
Error handlingTry-catch with JavascriptException and logging
Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
How to handle basic functionality using JavaScriptExecutor?How to automate CAPTCHA?
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105