WebDriver driver = new ChromeDriver();
1. Scroll to specific location
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(0,2000)");
2. Scroll to specific element
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
WebElement name = driver.findElement(By.id("name")); //Store element in object of WebElement
JS.executeScript("arguments[0].scrollIntoView(true)",name); //arguments[0] means first index of page starting at 0. Pass name object so it can scroll till that element.
3. Scroll to bottom of the page
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(0,document.body.scrollHeight");
4. Scroll Horizontally
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(2000,0)");
You can use "Option 2. Scroll to Specific element" if you are aware of element.
5. Slide Bar
WebElement slideBar = driver.findElement(By.xpath("//input[@name='slidebar'"));
slideBar.sendKeys(Keys.END);
6. Scroll dynamically loading pages
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
long previousHeight = (long)(JS.executeScript("return document.body.scrollHeight"));
While(true){
JS.executeScript("window.scrollTo(0,document.body.scrollHeight)");
Thread.sleep(2000);
long currentHeight = (long)(JS.executeScript("return document.body.scrollHeight"));
if(previousHeight == currentHeight)
break;
previousHeight = currentHeight;
}
1. Scroll to specific location
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(0,2000)");
2. Scroll to specific element
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
WebElement name = driver.findElement(By.id("name")); //Store element in object of WebElement
JS.executeScript("arguments[0].scrollIntoView(true)",name); //arguments[0] means first index of page starting at 0. Pass name object so it can scroll till that element.
3. Scroll to bottom of the page
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(0,document.body.scrollHeight");
4. Scroll Horizontally
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
JS.executeScript("window.scrollTo(2000,0)");
You can use "Option 2. Scroll to Specific element" if you are aware of element.
5. Slide Bar
WebElement slideBar = driver.findElement(By.xpath("//input[@name='slidebar'"));
slideBar.sendKeys(Keys.END);
6. Scroll dynamically loading pages
- Convert driver object to Javascript executor. This is called type casting.
JavascriptExecutor JS = (JavascriptExecutor)driver;
long previousHeight = (long)(JS.executeScript("return document.body.scrollHeight"));
While(true){
JS.executeScript("window.scrollTo(0,document.body.scrollHeight)");
Thread.sleep(2000);
long currentHeight = (long)(JS.executeScript("return document.body.scrollHeight"));
if(previousHeight == currentHeight)
break;
previousHeight = currentHeight;
}
No comments:
Post a Comment