Scroll Down-Up Web Page Using Javascript Executor In Selenium Webdriver

Yes, We tin scroll downward page using javascript executor In selenium webdriver. If yous remember, Earlier I conduct maintain posted many dissimilar posts where nosotros tin role javascript executor to perform dissimilar actions on spider web page similar Highlighting Element, Generating Alert, Get Domain Name, Get Page Title etc.. Now If yous needs to scroll downward the page together with hence yous tin use Javascript Executor In Selenium Webdriver test.

Why involve to scroll downward page ?
If yous run into unopen to websites peculiarly e-commerce website, More products volition hold upwards displayed solely If scroll downward the page. Same affair on facebook website. More posts volition hold upwards loaded solely If yous scroll downward the page. In this sort of scenario, You tin role javascript executor to scroll downward page If yous wants to conduct maintain unopen to activity on page chemical gene which appears on scroll down. You tin cheque presence of scroll equally described In THIS POST.

Scroll past times given pixel offset
I conduct maintain created evidence script to scroll down-up spider web page In horizontal or vertical management baser on given x y pixel offset.

Bellow given event volition kickoff scroll down(X direction) spider web page past times 600 pixels together with and hence It volition scroll upwards It past times 300 pixels.

package Testing_Pack;  import java.io.IOException; import java.util.concurrent.TimeUnit;  import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world degree Scrolling {    WebDriver driver;  @BeforeTest  world void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   driver.get("http://only-testing-blog.blogspot.in");  }    @Test  world void Scroll_Page() throws IOException, InterruptedException {   //To scroll downward spider web page past times 600 pixels In x(vertical) direction.     //You tin y parameter to scroll page In horizontal direction.   JavascriptExecutor javascript = (JavascriptExecutor) driver;   javascript.executeScript("window.scrollBy(0,600)", "");      Thread.sleep(3000);      //To scroll upwards spider web page past times 300 pixels In x(vertical) direction.   javascript.executeScript("window.scrollBy(0,-300)", "");   } }

Scroll downward to bottom of page
If yous wants to scroll downward to bottom of the page, You tin role bellow given @Test method In inwards a higher house example.

@Test  world void Scroll_Page() throws IOException, InterruptedException {   //Scroll downward to bottom of the page.   JavascriptExecutor javascript = (JavascriptExecutor) driver;   javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");    }

Scroll to chemical gene In selenium WebDriver
If yous wants to scroll till unopen to chemical gene on page together with hence yous tin role chemical gene parameter In javascript equally shown In bellow example.
//Scroll till element. JavascriptExecutor je = (JavascriptExecutor) driver; WebElement chemical gene = driver.findElement(By.xpath("//div[@id='dragdiv']")); je.executeScript("arguments[0].scrollIntoView(true);",element);

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post