jQuery is the most famous method that most of the web designers/ developers are using these days. To make the web application looks better, either make it fancy or us er friendly most of the people tend to use these. Mouse over events are one of the famous events outta jQueries.
When accessing these types of Mouse over elements using xpath we cannot directly access them because it is triggered through an event. So we have to deal with them in different way. Below code is explaining how we can click on a sub menu which expanding on jQuery trigger. You can simply cope and paste this code in your java tool and test your self.
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.interactions.Actions;
public class Test3 {
public static void main(String[] args) throws Exception {
//Creates a web driver object
WebDriver A = new FirefoxDriver();
//Loads the URL
A.get("http://www.johnlewis.com/");
//Locate the main menu and assign the xpath location to a variable
WebElement MouseOverLink = A.findElement(By.xpath("//a[text()='Christmas']"));
//Creates an object from Action class
Actions Action_01 = new Actions(A);
//perform moveToeElement() method and pass the variable value
Action_01.moveToElement(MouseOverLink).perform();
//locate the Sub Menu and assign the xpath location to a variable
WebElement MouseOverLink2 = A.findElement(By.xpath("//a[@href='/christmas/new-for-2013/c6000450511']"));
//Perform moveToeElement() method and pass the variable value
Action_01.moveToElement(MouseOverLink2).perform();
//Click on the sub menu
Action_01.click().perform();
}
}
When we are dealing with the web elements the "Actions" class is very important. To learn about the methods and functions inside the "Actions" class ref below link. Read more about Actions class in java.
In this example i'm using only the "moveToeElement()" method which is capable enough to move the mouse over to given element either by the xpath or a defined variable. This is very important when we have mouse over elements.
"click().perform()" These 02 commands are same like the previous examples, Where as we can perform the click function to the given element.
No comments:
Post a Comment