Automation, Automation Framework, Java, Java Class, javaScript, Selenium, Testing, Web Automation

JavaScript elements handling in selenium

As we see most of the applications are moving towards development with Angular, React, Aurelia, JS, TS bases scripting language and framework which uses these scripting languages.

When it comes to Quality assurance there is still some project which has not adopted a new framework based on these scripting languages.

For those, we have to interact with the JavaScript element multiple times just to make a tweak in the code. As DOM is very responsive and GUI is fragile we need to make those adjustments in the code. Here are some ways via which we can carry out that in a project which uses Selenium and Java together.

How to click on a javascript element in selenium?

public static void click_JS(WebElement locator) throws InterruptedException, IOException
{
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("arguments[0].click();", locator);
}

How to send data in JS object in selenium?

public static void sendkeysbyID_JS(String locator, String value) throws InterruptedException, IOException
{
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("document.getElementById('" + locator + "').value = '" + value + "';");
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s