我试图将鼠标悬停在主菜单上并使用 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/