Selenium Action类没有在同一个webelement上第二次鼠标悬停

问题描述 投票:0回答:4
Actions action = new Actions(Driver);
action.MoveToElement(webelement).ClickAndHold().Perform();

我使用上面的代码鼠标悬停Web元素。它工作正常。当我尝试使用上面的代码第二次对同一个Web元素进行鼠标悬停时,它无法正常工作。

他们为什么鼠标悬停第二次没有工作的原因是什么?

javascript java c# selenium selenium-webdriver
4个回答
0
投票

您可能需要释放鼠标,如下所示。

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

如果您只想将鼠标悬停,则无需单击并按住。您可以使用以下代码将鼠标悬停在元素上。

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

0
投票

你可以尝试这种方法,我正在使用它,它每次都适合我:

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

        }

0
投票

当您尝试使用Mouse Hover类中的ClickAndHold()第二次Actions时,如下所示:

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

clickAndHold()方法基本上Clicks (without releasing) at the current mouse location.。所以它可能对我们没有帮助。但只是Mouse Hover你不需要Click and Hold。我们可以简单地MoveToElement并调用Perform()来实现如下相同:

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

0
投票

你正在使用的ClickAndHold()方法,不释放鼠标。 尝试使用以下代码:

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
© www.soinside.com 2019 - 2024. All rights reserved.