Selenium webdriver 无法在selenium webdriver中执行鼠标悬停和单击

Selenium webdriver 无法在selenium webdriver中执行鼠标悬停和单击,selenium-webdriver,Selenium Webdriver,我想在java中使用SeleniumWebDriver对元素执行mouseover函数。我使用的代码是: Actions actions = new Actions(driver); WebElement menuHoverLink = driver.findElement(By.xpath(".//a[@title='Directory'][.='People']")); actions.moveToElement(menuHoverLink).build().perform(); WebEl

我想在java中使用SeleniumWebDriver对元素执行mouseover函数。我使用的代码是:

Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.xpath(".//a[@title='Directory'][.='People']"));
actions.moveToElement(menuHoverLink).build().perform();
WebElement subLink = driver.findElement(By.xpath(".//*[@id='peopleSubmenu']/ul/li[1]/a"));
subLink.click();

但是当鼠标移动时,代码不起作用。请给我建议一些解决方案

直接点击子链接,在子链接上点击鼠标

actions.moveToElement(subLink).click().build().perform();
编辑:注意注释,删除子链接xpath中的。(点)

WebElement subLink = driver.findElement(By.xpath("//*[@id='peopleSubmenu']/ul/li[1]/a"));

试试这个,这只是维格尼什在下面发布的另一种形式

new Actions(driver).moveToElement(menuHoverLink).perform();
new Actions(driver).moveToElement(subLink).perform();
subLink.click();

运行此代码时会发生什么。同时发布HTML内容。删除XPath开头的
。当鼠标光标不在浏览器屏幕上时,我可以执行鼠标移动。如果光标移动,鼠标悬停会持续几秒钟。即使移动鼠标,我也要执行鼠标悬停。“即使移动鼠标,我也要执行鼠标悬停。”这是什么意思?顺便说一句,您是否按照建议更正了xpath?@VarshaJawale您遇到了什么异常?在哪一行?我没有收到任何异常,但鼠标悬停仅持续几秒钟。请尝试在鼠标悬停和单击之间加入Thread.sleep 1000毫秒。此处不存在等待问题。。我已经试过了,那原因是什么?