top of page
  • Writer's picturekhyati sehgal

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 + "';");
}
0 views0 comments

Recent Posts

See All
bottom of page