Do you need scroll the page ? (Selenium and javascript)

If you need scroll the page for example take a screenshot in a browser different of Firefox (Yes, the selenium have a problem to take a screenshot of the entire page when you are in a different browser like, IE)

So we have to create a function that take a screenshot of parts of the page, and this function helps you to scroll the page.

First you have to import the libraries:

import org.openqa.selenium.JavascriptExecutor;

 

After the code:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollTo(0,Math.max(document.documentElement.scrollHeight,”
+ “document.body.scrollHeight,document.documentElement.clientHeight));”);

 

After this, the driver will scroll until the end of the current page.

I hope that I help you 🙂

Mark element in the page with red border (Javascript with Selenium)

Hello again, now I will give a tip of Selenium, for mark the element that you want and before this, you can take a screenshot for evidence.

First you have to import this libraries:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

 

After this, you have to call the javascript executor and give the id or other thing for find the element:

JavascriptExecutor js = (JavascriptExecutor) driver; // change the name of your WebDriver
WebElement element = driver.findElement(By.id(“element“)); // change the localizator 

js.executeScript(“arguments[0].style.border=’3px solid red'”, element); // change the name of your variable

 

This is very usefull for mark the element in the page.

So, just this ! Bye bye 🙂