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();
}
What is the use of setEnableNativeEvents?
ReplyDeleteEnabling features that are disabled by default in Firefox
ReplyDeleteNative 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);
WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));
ReplyDeleteXPath used in above code is taken from which element.
The element which you want to drag from its current position to somewhere else by its xoffset and yoffset value.
DeleteAbove code is not working for me. I am getting Exception. Please do the needfull
ReplyDeletethere are many ways : to scroll the scroll bar
Deleteyes above code is not working for me as wel ,
ReplyDeleteTry using ID or name..xpath may be dynamically generated.so it may not work i think..
ReplyDeletetry using ID or name.Xpath may be a dynamic one.
ReplyDeleteAbove 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")));
ReplyDeleteRanjit
I think you need to switch to //iframe[@class="demo-frame"], before working on Element //div[@id='draggable']"
ReplyDeleteIn the jqueryui.com website, the webelement (id='draggable') is placed under a different frame.
ReplyDeleteso try using the below statement after the navigate statement.
driver.switchTo().frame(driver.findElement(By.className("demo-frame")));
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".
ReplyDeleteAdd the below statement after thread.sleep then check
driver.switchTo().frame(browser.findElement(By.className("demo-frame")));
FirefoxProfile profile=new FirefoxProfile();
ReplyDeleteprofile.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.
actn.dragAndDropBy(element, 50, 50).build().perform();
ReplyDeleteGuys in the above sample code xpath is given wrong..
ReplyDeleteOnly asterisk is not there that's why it gives error. Try this.
"//*[@id='draggable']"
import java.util.Set;
ReplyDeleteimport 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
why would you need to scroll web element?
ReplyDeleteThis is a topic that is near to my heart... Many thanks! Exactly where are your contact details though?
ReplyDeleteI believe tһіs іs amog the moѕt significant info fߋr me.
ReplyDeleteAnd 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
Remarkable things here. I am very happy to see your
ReplyDeletearticle. Thank you a lot and I'm having a look forward
to touch you. Will you kindly drop me a mail?