Wednesday 16 December 2015

Page UP/DOWN, Scroll, Move to Element in Selenium


// Moving cursor in top left corner using JavascriptExecutor

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 0);");

Ref: https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

// Page UP Using Keys
@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

saveButton.sendKeys(Keys.PAGE_UP);


// Move to Element Using Actions.

@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

Actions actions = new Actions(driver);
actions.moveToElement(saveButton);
actions.perform(); 

Ref: https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html#moveByOffset-int-int-

// Move to Specific  Element Using JavascriptExecutor.

@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

1) 
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", saveButton); 

2)
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"
saveButton.getLocation().y + ")");