将鼠标悬停在菜单上,然后在java Selenium中选择子菜单

时间:2017-01-15 21:34:42

标签: java selenium selenium-chromedriver

我试图将鼠标悬停在主菜单上并使用java selenium选择一个子菜单,我让它悬停在菜单上但不能选择子菜单,如果我尝试通过linktext找到我总是得到错误“不存在“如果我使用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();

7 个答案:

答案 0 :(得分:1)

在WebDriver中,我们提供了控制Mouse事件的选项。试试这段代码。这应该有助于达到目的。

driver.get("https://www.skiutah.com/");
WebElement deals = driver.findElement(By.xpath("//a[@title='Deals']"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
Locatable hoverItem = (Locatable) deals;
mouse.mouseMove(hoverItem.getCoordinates());
WebElement beginner = driver.findElement(By.xpath("//a[text()='Beginner']"));
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(beginner));
Locatable clickItem = (Locatable) beginner;
mouse.mouseDown(clickItem.getCoordinates());
mouse.mouseUp(clickItem.getCoordinates());
System.out.println(driver.getTitle());

答案 1 :(得分:1)

要使用鼠标悬停操作,我们需要使用build.perform。它被称为动作链,确保它最终在一起执行动作。或者您可以按如下方式交换该行,它应该适合您。我试过看起来不错。

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(el).click();
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(el).click();
action.moveToElement(element).perform();

答案 2 :(得分:0)

我认为您正在徘徊并点击子菜单的方式似乎不正确。

您尚未共享html,因此检查您找到的元素是否正确有点乏味。如果一切正常,请尝试以下可能对您有帮助的代码 -

WebElement menu =  driver.findElement(By.your_locator);
WebElement sub_menu =  driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();

说明: -

此处build()方法用于将所有操作列表编译为一个步骤并准备执行

答案 3 :(得分:0)

//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();

答案 4 :(得分:0)

This works for me first time, but if repeated for other menu item then it cant find or something.

WebElement menu =  driver.findElement(By.your_locator);
WebElement sub_menu =  driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();

答案 5 :(得分:0)

首先将鼠标悬停在主菜单上,然后单击任何子菜单。

WebDriverWait Wait = new WebDriverWait(driver,10);
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[@id='top_menu']/ul/li[4]/a"))));
Actions mousehover = new Actions(driver);
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform();
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals"))));
driver.findElement(By.linkText("All Deals")).click();

答案 6 :(得分:0)

我试图点击'子菜单。需要将鼠标移到主菜单上,加载子菜单需要几秒钟。然后我需要找到子菜单并单击它。 这是我使用的代码

 Actions ac = new Actions(dr);  
 WebElement we = dr.findElement(By.xpath(".//*[@id='ddtopmenubar']/ul/li[1]/a"));
 ac.moveToElement(we).build().perform();
 WebDriverWait wait = new WebDriverWait(dr, 5); 
 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a")));
 WebElement e= dr.findElement(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a"));
 e.click();

但它似乎没有成功。

获得例外: org.openqa.selenium.WebDriverException:performActions 构建信息:版本:'未知',修订版:'未知',时间:'未知'

当我在调试模式下执行相同操作时,我可以单击子菜单。