Monday 21 November 2011

Moving a mouse on a Object and right clicking on it in Selenium Webdriver using CSharp



IWebElement element = driver.FindElement(By.XPath("//img[@name='jack']"));
new Actions(driver).MoveToElement(element).Perform();
ILocatable loc = (ILocatable)element;
IMouse mouse = ((IHasInputDevices)driver).Mouse;         // Converting driver as a Mouse
mouse.ContextClick(loc.Coordinates);                     //Right Click
IKeyboard key = ((IHasInputDevices)driver).Keyboard;    // Converting driver as a Keyboard
key.SendKeys("w");

3 comments:

  1. Hi Anjaneyulu,

    Is it possible to send the code by using JAVA. I have tried a lot but no use.

    my requirement is i need to right click on a selected item and i want to select any of the option in the right click context menu options.

    ReplyDelete
  2. Java Equivalent:

    Actions action= new Actions(this.driver);
    action.moveToElement(element).perform();


    Locatable loc = (Locatable)element;
    Mouse mouse = ((HasInputDevices)this.driver).getMouse(); // Converting driver as a Mouse
    mouse.contextClick(loc.getCoordinates()); //Right Click
    Keyboard key = ((HasInputDevices)this.driver).getKeyboard(); // Converting driver as a Keyboard
    key.sendKeys("w");

    ReplyDelete
  3. Anyone had any luck with this? I'm using Java and i would like it to right click on a flash game but the right click only wants to open in the top left of the browser. Any tips would be appreciated.

    ReplyDelete