简体   繁体   中英

Selenium Action class is not doing mouse hover for the second time on same webelement

Actions action = new Actions(Driver);
action.MoveToElement(webelement).ClickAndHold().Perform();

I have used the above code to mouse hover the Web Element. It is working fine. When I try to do mousehover for the same Web Element second time using the above code it is not working.

Is their any reason why mousehover is not working second time?

You may need to release the mouse holding as given below.

Actions action = new Actions(Driver); 
action.MoveToElement(webelement).ClickAndHold().Release().Perform();

And No need to click and hold if you only want to mouse over. You can mouse over on a element with following code.

Actions action = new Actions(Driver); 
action.MoveToElement(webelement).perform();

ClickAndHold() method that you're using, is not releasing the the mouse.

Try to use below code:

WebElement elem = driver.FindElement(--USE YOUR BY ELEMENT HERE--);

Actions builder = new Actions(driver);  
builder.MoveToElement(elem).Perform();//this will hover to YOUR ELEMENT
Thread.Sleep(1000);                   //avoid using this type of wait. Try to use wait until.

driver.FindElement(--USE HERE YOUR SEPECIFIC ELEMENT--).Click();  //this will click on SEPECIFIC ELEMENT

You can try this method ,I am using it and it worked for me every time :

public static void moveTo(WebElement element){
    Actions action=new Actions(BrowserUtilities.driver);
    action.moveToElement(element).build().perform();

        }

As you are trying to Mouse Hover by using ClickAndHold() from the Actions Class for the second time as follows :

Actions action = new Actions(Driver);
action.MoveToElement(webelement).ClickAndHold().Perform();

clickAndHold() method basically Clicks (without releasing) at the current mouse location. . So it may not help us. But to simply Mouse Hover you don't need to Click and Hold . We can simply MoveToElement and invoke Perform() to achieve the same as follows :

new Actions(Driver).MoveToElement(webelement).Perform();

This code is in python you can use same feature in your language #First time use normally

actions = ActionChains(self.driver)
actions.move_to_element(admin_tab).perform()

#second time use reset action before using mouse hover

actions.reset_actions()# to reset mouse hover
actions = ActionChains(self.driver)
actions.move_to_element(admin_tab_new).perform()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM