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)", "");
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+")", "");
}
{
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);
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)");
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)");
}
{
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);");
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("arguments[0].scrollIntoView();", element);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, -1000000000)");
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);
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);
}
{
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;
}
{
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);
}
{
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);
}