Actions action = new Actions(Driver);
action.MoveToElement(webelement).ClickAndHold().Perform();
我使用上面的代码鼠标悬停Web元素。它工作正常。当我尝试使用上面的代码第二次对同一个Web元素进行鼠标悬停时,它无法正常工作。
他们为什么鼠标悬停第二次没有工作的原因是什么?
您可能需要释放鼠标,如下所示。
Actions action = new Actions(Driver);
action.MoveToElement(webelement).ClickAndHold().Release().Perform();
如果您只想将鼠标悬停,则无需单击并按住。您可以使用以下代码将鼠标悬停在元素上。
Actions action = new Actions(Driver);
action.MoveToElement(webelement).perform();
你可以尝试这种方法,我正在使用它,它每次都适合我:
public static void moveTo(WebElement element){
Actions action=new Actions(BrowserUtilities.driver);
action.moveToElement(element).build().perform();
}
当您尝试使用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();
你正在使用的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