Richard Hansson
Richard Hansson

Reputation: 1

Selenium objects out of the screen

I currently have a selenium test that runs smooth on 1920 * 1080 resolution. But I have a task to make this test on different common resolutions such as 1366 *768.

Problem is when I run my Selenium test on smaller resolutions than 1920 * 1080 I can't find some elements that is below the window (as expected) How do I solve this?

I've tried JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");

To scroll to the bottom of the page but with no success. Would appreciate help loads. Using java, Selenium, TestNG and POM.

Upvotes: 0

Views: 782

Answers (2)

noor
noor

Reputation: 3004

to scroll bottom of page, use the below code:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,300)", "");

or

js.executeScript("scroll(0, 300);");

or

js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); 

Upvotes: 0

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi to scroll please use like below

Scrolling to bottom of a page

driver.navigate().to(URL);
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Scrolling to an element on a page

driver.navigate().to(URL);
WebElement element = driver.findElement(By.id("id"));
((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);

Scrolling by coordinates

 driver.navigate().to(URL);
 ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

Upvotes: 0

Related Questions