java - 将鼠标悬停在菜单上并选择 java Selenium 中的子菜单

标签 java selenium selenium-chromedriver

我试图将鼠标悬停在主菜单上并使用 java selenium 选择子菜单,我让它悬停在菜单上但无法选择子菜单,如果我尝试通过链接文本查找我总是得到错误“不存在”如果我使用 xpath 表示构建成功但不会打开新页面。到目前为止,这是我的代码

System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe");      
    WebDriver webDriver = new ChromeDriver();       
    webDriver.manage().window().maximize();
    webDriver.navigate().to("https://www.skiutah.com");

    String NavTo = "DEALS";
    String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
    WebElement element = webDriver.findElement(By.linkText(NavTo));
    WebElement el = webDriver.findElement(By.xpath(pathx));
    Actions action = new Actions(webDriver);
    action.moveToElement(element).perform();
    action.moveToElement(el).click();

最佳答案

//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.linkText("Deals"));
//Initiate mouse action using Actions class
Actions builder = new Actions(driver);
// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();
// wait for max of 5 seconds before proceeding.
// until this submenu is found
WebDriverWait wait = new WebDriverWait(driver, 5); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a")));  
//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a"));
menuOption.click();

关于java - 将鼠标悬停在菜单上并选择 java Selenium 中的子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666569/

相关文章:

java - java中的简单日期格式,解析异常

java - 如何暂停/恢复使用 mediarecorder 创建的录音?

linux - 如何在 Alpine Linux 上运行 Angular6 E2E 测试

java - JScrollPane 不显示在 JTextArea 上

java - Go SHA-256 哈希不同于 Java SHA-256 哈希

python-3.x - 如何使用 Selenium 在youtube中获得所有评论?

python selenium 无法点击标签

javascript - FireFoxDriver JavaScript 错误不显示在 FireFox 浏览器中

python - 使用 python 中的 selenium 抓取 youtube 上的所有评论及其回复

Selenium 和 ChromeDriver : session not created, 无法连接到渲染器