Sometimes there is a need of locating an element while scrolling a page in functional automation. As no method provided by Selenium WebDriver to scroll a web page. So, we require a java script code which can be executed by JavascriptExecutor Interface which provides a way to execute Java Script code using Selenium WebDriver.
To perform a scrolling feature I am taking an example of Amazon.in where I am scrolling a page until we find "Instagram' link which is available at the bottom of the web page.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class scrollFeature
{
public static WebDriver driver;
public static JavascriptExecutor js;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
js=((JavascriptExecutor)driver);
driver.get("http://www.Amazon.in");
Thread.sleep(5000);
WebElement txt=driver.findElement(By.xpath("//*[text()='Instagram']"));
js.executeScript("arguments[0].scrollIntoView()",txt);
js.executeScript("arguments[0].setAttribute('style','border:solid red;');", txt);
}
}
Output of the above code
To perform a scrolling feature I am taking an example of Amazon.in where I am scrolling a page until we find "Instagram' link which is available at the bottom of the web page.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class scrollFeature
{
public static WebDriver driver;
public static JavascriptExecutor js;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
js=((JavascriptExecutor)driver);
driver.get("http://www.Amazon.in");
Thread.sleep(5000);
WebElement txt=driver.findElement(By.xpath("//*[text()='Instagram']"));
js.executeScript("arguments[0].scrollIntoView()",txt);
js.executeScript("arguments[0].setAttribute('style','border:solid red;');", txt);
}
}
Output of the above code
No comments:
Post a Comment