Tuesday 7 January 2014

How to scroll web element

    How to scroll web element?--not browser—

FirefoxProfile profile=new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver=new FirefoxDriver(profile);
driver.navigate("http://jqueryui.com/draggable/");
Thread.sleep(6000L);
WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));
Actions actn=new Actions(driver);
actn.dragAndDropBy(element, 50, 50).build().perform();
}

21 comments:

  1. What is the use of setEnableNativeEvents?

    ReplyDelete
  2. Enabling features that are disabled by default in Firefox
    Native events is such a feature: It is disabled by default for Firefox on Linux as it may cause tests which open many windows in parallel to be unreliable. However, native events work quite well otherwise and are essential for some of the new actions of the Advanced User Interaction. To enable them:

    FirefoxProfile profile = new FirefoxProfile();
    profile.setEnableNativeEvents(true);
    WebDriver driver = new FirefoxDriver(profile);

    ReplyDelete
  3. WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));

    XPath used in above code is taken from which element.

    ReplyDelete
    Replies
    1. The element which you want to drag from its current position to somewhere else by its xoffset and yoffset value.

      Delete
  4. Above code is not working for me. I am getting Exception. Please do the needfull

    ReplyDelete
    Replies
    1. there are many ways : to scroll the scroll bar

      Delete
  5. yes above code is not working for me as wel ,

    ReplyDelete
  6. Try using ID or name..xpath may be dynamically generated.so it may not work i think..

    ReplyDelete
  7. try using ID or name.Xpath may be a dynamic one.

    ReplyDelete
  8. Above Draggable element is inside a iframe so before identifying it, you need to switch to frame .Use this code before identifying element: driver.switchTo().frame(driver.findElement(By.cssSelector(".demo-frame")));

    Ranjit

    ReplyDelete
  9. I think you need to switch to //iframe[@class="demo-frame"], before working on Element //div[@id='draggable']"

    ReplyDelete
  10. In the jqueryui.com website, the webelement (id='draggable') is placed under a different frame.
    so try using the below statement after the navigate statement.
    driver.switchTo().frame(driver.findElement(By.className("demo-frame")));

    ReplyDelete
  11. Its because that particular element is inside a Frame, so if we have to perform any Action on that element initially we need to switch to that "Frame".

    Add the below statement after thread.sleep then check

    driver.switchTo().frame(browser.findElement(By.className("demo-frame")));

    ReplyDelete
  12. FirefoxProfile profile=new FirefoxProfile();
    profile.setEnableNativeEvents(true);
    WebDriver driver=new FirefoxDriver(profile);
    driver.manage().window().maximize();
    driver.get("http://jqueryui.com/draggable/");
    //The element is inside iFrame. So we need to switch the frame before we can find the element.
    driver.switchTo().frame(driver.findElement(By.xpath("//*[@class='demo-frame']")));
    WebElement element= driver.findElement(By.xpath("//html/body/div/p"));
    Actions actn=new Actions(driver);
    actn.dragAndDropBy(element, 50,50).perform();
    -------------------------------
    The above code works.

    ReplyDelete
  13. actn.dragAndDropBy(element, 50, 50).build().perform();

    ReplyDelete
  14. Guys in the above sample code xpath is given wrong..
    Only asterisk is not there that's why it gives error. Try this.

    "//*[@id='draggable']"

    ReplyDelete
  15. import java.util.Set;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.interactions.Actions;

    public class SampleClass {

    public static void main(String[] args) throws InterruptedException {
    FirefoxProfile profile=new FirefoxProfile();
    profile.setEnableNativeEvents(true);
    WebDriver driver=new FirefoxDriver(profile);
    driver.manage().window().maximize();
    driver.get("http://jqueryui.com/draggable/");
    Thread.sleep(4000L);
    driver.switchTo().frame(0);
    WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));
    Actions actn=new Actions(driver);
    actn.dragAndDropBy(element, 100, 100).build().perform();
    Thread.sleep(2000);
    driver.quit();
    }
    }


    Try this code too...it will work

    ReplyDelete
  16. why would you need to scroll web element?

    ReplyDelete
  17. This is a topic that is near to my heart... Many thanks! Exactly where are your contact details though?

    ReplyDelete
  18. I believe tһіs іs amog the moѕt significant info fߋr me.
    And i'm glad reading үour article. Hoԝevеr wahna commentary
    оn sⲟme basic issues, Ꭲhe web site taste іs perfect, the articles
    іs in reality nice : Ɗ. Juust rigһt process, cheers

    ReplyDelete
  19. Remarkable things here. I am very happy to see your
    article. Thank you a lot and I'm having a look forward
    to touch you. Will you kindly drop me a mail?

    ReplyDelete