How to right click on webement on the page using selenium webdriver?

by Selenium Trainer

in Blog, SELENIUM WEB DRIVER

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.internal.EventFiringMouse;

public class TestEventFiringMouse {

	public static void main(String[] args) throws InterruptedException {

		WebDriver FF_driver = new FirefoxDriver();

		// EventFiringWebDriver(WebDriver driver) is Constructor of Class EventFiringWebDriver 
		// http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/events/EventFiringWebDriver.html
		EventFiringWebDriver myTestDriver = new EventFiringWebDriver(FF_driver);
		myTestDriver.manage().window().maximize();

		MyListenerMethodsImplementation myListener = new MyListenerMethodsImplementation();
		myTestDriver.register(myListener);

//		Constructor Summary
//		EventFiringMouse(WebDriver driver, WebDriverEventListener dispatcher) 
		EventFiringMouse testMouseMovement = new EventFiringMouse(myTestDriver , myListener);

		myTestDriver.get("http://in.news.yahoo.com/");

		// Move Mouse to that link object
		// http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html?com/thoughtworks/selenium/package-summary.html
		Locatable hoverItem = (Locatable) myTestDriver.findElement(By.xpath("//*[@id='mediasectionlist_8']/div[1]/h3/a/em"));

		// http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/interactions/internal/Coordinates.html
		Coordinates MyTestCoordinates = hoverItem.getCoordinates();

		try{
			testMouseMovement.mouseMove(MyTestCoordinates);
		}catch(Exception e1){

			System.out.println(e1);
		}
		Thread.sleep(50000L);

		// Move mouse to webelement and right click on that
		// Shift+F10 or Menu Key: Opens Shortcut Menu (same as right-clicking)
		//http://www.zeigen.com/shortcuts/2007/03/01/shortcut-menu/

		myTestDriver.findElement(By.xpath("//*[@id='mediasectionlist_7']/div[1]/h3/a/em")).sendKeys(Keys.chord(Keys.SHIFT,Keys.F10));

		Thread.sleep(50000L);
		// Move mouse to Webelement and get coordinates

		Point MyPoint= myTestDriver.findElement(By.xpath("//*[@id='mediasectionlist_6']/div[1]/h3/a/em")).getLocation();
		System.out.println(MyPoint.x);

		System.out.println(MyPoint.y);

	}

}










Reference http://in.news.yahoo.com/

Reference http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/events/EventFiringWebDriver.html

Previous post:

Next post: