MainMenu

Home Java Overview Maven Tutorials

Monday, 16 May 2022

JavascriptExecutor usage in selenium

In this article we will see how to perform multiple operations like scroll, click, getText, sendText using java script executor

If a web page is highly dynamic in nature it have complex Javascript functions or Ajax function then sometimes simple click using selenium like
driver.findElement(By.xpath("")).click(); do not work OR many functions like getText(), sendKeys() etc do not work
So here we can use JavascriptExecutorInterface and its various mathods to perform such operations.

How to perform Scroll by using java script executor

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(X,Y)", "");
//here X & Y are value in pixal

Example :- JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");


public static void scrollxandy(WebDriver driver, int x, int y)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy("+x+","+y+")", "");
}


How to perform Scroll upto a webElement by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


How to perform Scroll upto upto bottom of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");



public static void scrolluptobottom(WebDriver driver)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}


How to perform Scroll upto upto Top of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, 0);");


WebElement element = driver.findElement(By.tagName("header"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, -1000000000)");


How to perform Action click by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", we);


How to perform Action SendKeys/SendText by using java script executor


public static void sendTextusingJavascriptExecutor(WebDriver driver, WebElement we, String text)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value=arguments[1];", we);
}



How to perform Action getText by using java script executor


public static String getTextUsingJavascriptExecutor(WebDriver driver, WebElement element)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
String text = (String) jse.executeScript("return arguments[0].value", element);
return text;
}




How to Highlight a webElement by using java script executor


public static void highlight(WebDriver driver, WebElement element) throws Exception
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
Thread.sleep(100);
jse.executeScript("argument[0].setAtrribute('style', '');", element);
Thread.sleep(100);
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
}




No comments:

Post a Comment