How to code mouse hover with Selenium Webdriver

I was trying to code for mousehover
and i actually followed someone’s code and to me
its not working

How to code mouse hover with Selenium Webdriver

also the I can’t really find the element id for

How to code mouse hover with Selenium Webdriver


for this part
there is no id specified

Add Comment
  • 3 Answer(s)

    To use ID it has to be present in the element’s html. In the image shown, highlighted element, there is no id. The only valid attribute is class. I would suggest you find this element using Xpath. Also the correct syntax is
    driver.findElement(By.id(“HM_Item2_4”)); you dont  have to specify the id= in it. However in  this case I would suggest use Xpath.

    You can take a look at the tutorial here
    http://www.toolsqa.com/selenium-webdriver/finding-elements-using-browser-inspector/

    Virender

    Professor Answered on July 14, 2015.
    Add Comment

    Hi
    Let me show you the code for mouse hover

    //navigate index page
    driver.get(“http://www.flipkart.com/”);

    //hover electronics tab
    WebElement moveonmenu=driver.findElement(By.xpath(“.//*[@id=’fk-mainhead-id’]/div[2]/div/div/ul/li[1]/a/span”));
    Actions actions=new Actions(driver);
    actions.moveToElement(moveonmenu).perform();
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //hover and click Mi from Mobiles
    WebElement mi=driver.findElement(By.xpath(“.//*[@id=’menu-electronics-tab-0-content’]/ul[1]/li[5]/a”));
    actions.moveToElement(mi).click().perform();

    If you have any question just ask
    Janani

    Train Answered on July 14, 2015.
    Add Comment

    you can also do it by following

    Actions act=new Actions(driver);
    act.moveToElement(driver.findElement(By.xpath(“XPATH]”)));
    act.build().perform();

    where XPATH is the xpath of the element you want to hover the mouse to.
    Hope this helps.

    Default Answered on July 15, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.