What is JavaScriptExecutor: in Selenium allows you to run JavaScript in the browser. It's useful when WebDriver can't interact with certain elements or handle more complex tasks, like working with hidden or dynamic elements.
Why Use JavaScriptExecutor:
✓It's useful when WebDriver can't interact with certain elements or handle more complex tasks, like working with hidden or dynamic elements.
✓JavaScriptExecutor lets you make changes to the web page directly, providing more control over its behavior.
How to Implement JavaScriptExecutor:
To use JavaScriptExecutor, you cast your WebDriver instance to the JavascriptExecutor interface:
JavascriptExecutor js = (JavascriptExecutor) driver;
Running JavaScript for Custom Actions
Using executeScript(): method allows you to run JavaScript code within the browser.
Syntax:
js.executeScript("alert('Hello Everyone.');");
You can also send WebElements or other parameters to your JavaScript.
Example:
WebElement element = driver.findElement(By.id("elementId"));
js.executeScript("arguments[0].click();", element)
Scrolling Pages Using JavaScript
When elements are not visible within the current viewport, you can use JavaScript to scroll the web page. For Example
*Scroll to a Specific Element:: This scrolls the page until the specified element is visible.
WebElement element = driver.findElement(By.id("elementId"));
js.executeScript("arguments[0].scrollIntoView(true);", element);
*Scroll by a Number of Pixels: To scroll the page by a specific number of pixels vertically or horizontally.
// Scroll down by 1000 pixels
js.executeScript("window.scrollBy(0, 1000);");
// Scroll horizontally by 500 pixels
js.executeScript("window.scrollBy(500, 0);");
*Scroll to the Bottom of the Page: To go to the bottom of the page.
js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
*Scrolling to the Top of the Page: To return to the top of the page.
js.executeScript("window.scrollTo(0, 0);");
Clicking Hidden Elements:
JavaScriptExecutor can be used to click elements that are hidden or overlapped by other elements, which WebDriver might fail to click.
Example:
WebElement element = driver.findElement(By.id("hiddenElement"));
js.executeScript("arguments[0].click();", element);
Fetching Web Element Properties with JavaScript
Extracting Element Attributes:
JavaScriptExecutor you can access and retrieve element attributes or properties that may not be easily accessible through standard WebDriver methods.
JavaScriptExecutor lets you get various properties of web elements:
*Get an Element's Attribute: If you need to access an attribute from a web element (such as value, id, name, or custom attributes), JavaScriptExecutor allows you to directly extract it from the DOM.
// Locating the element
WebElement element = driver.findElement(By.id("elementId"));
// Retrieving the attribute using JavaScriptExecutor
String attributeValue = (String) js.executeScript("return arguments[0].getAttribute('attributeName');", element);
*Retrieve Element Text:
getText() can capture visible text, you might need to access text that is hidden or presented differently in the DOM. In such scenarios, JavaScriptExecutor’s properties like innerText or textContent can be used to retrieve the text.
String elementText = (String) js.executeScript("return arguments[0].innerText;", element);
*innerText gives the visible text of the element.
*textContent retrieves all text inside the element, even if it’s hidden.
*Get Inner HTML:
String innerHTML = (String) js.executeScript("return arguments[0].innerHTML;", element);
*Get Window Size:
Long width = (Long) js.executeScript("return window.innerWidth;");
Long height = (Long) js.executeScript("return window.innerHeight;");
*Get Current URL:
String url = (String) js.executeScript("return document.URL;");
*Get Page Title:
String title = (String) js.executeScript("return document.title;");
Enabling Disabled Elements:
Sometimes, users may need to interact with buttons or input fields that are disabled. JavaScript Executor can modify the disabled property of these elements to make them interactable.
WebElement button = driver.findElement(By.id("disabledButton"));
js.executeScript("arguments[0].disabled = false;", button);
Triggering Events via JavaScript:
JavaScript Executor can be used to simulate various events on elements, including hovering or triggering custom JavaScript events.
WebElement element = driver.findElement(By.id("hoverElement"));
js.executeScript("var event = new MouseEvent('mouseover', { bubbles: true }); arguments[0].dispatchEvent(event);", element);
Conclusion
JavaScriptExecutor is a powerful tool in Selenium that enhances test automation capabilities by enabling direct interaction with the DOM. Its versatility makes it a valuable asset for handling complex scenarios that are otherwise challenging to achieve using standard WebDriver methods.
Comments
Post a Comment