Xpath无法在Selenium中使用MouseOver(CatMenu)

时间:2017-03-04 10:34:00

标签: selenium testing xpath intellij-idea mouseover

对于无法打开鼠标悬停菜单的所有网页驱动程序存在关于硒的问题。尽管对元素网络驱动程序的Xpath的帮助无法打开鼠标悬停(CatMenu)并且日志是“必须提供移动操作的位置”。

我想去n11.com网址,然后在Kitap,Müzik,Film,Oyun上点击Kitap,但它不起作用。 谢谢

@Test

public void  startWebDriver(){
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.n11.com");


Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath(".//*[@id='contentMain']/div/nav/ul/li[8]/a"))).perform();

}

2 个答案:

答案 0 :(得分:1)

使用以下代码实现相同的目标 -

    System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
    driver =new ChromeDriver();     
    driver.get("http://www.n11.com/");
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    WebElement menu =  driver.findElement(By.xpath("//li[@class='catMenuItem']/a[@title='Kitap, Müzik, Film, Oyun']"));
    WebElement submenu = driver.findElement(By.xpath("//li[@class='subCatMenuItem']/a[@title='Kitap']"));

    Actions action = new Actions(driver);

    action.moveToElement(menu).moveToElement(submenu).click().build().perform();

使用一些Implicit Wait来避免超时异常以找到您的网络元素
使用更具体的xpath来查找您的网络元素。

首先,您需要将鼠标悬停在Kitap, Müzik, Film, Oyun菜单上,然后点击Kitap子菜单

答案 1 :(得分:0)

您可以尝试使用ExplicitWait更具体的XPath

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[@title='Kitap, Müzik, Film, Oyun']")[2])));
// WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Kitap, Müzik, Film, Oyun")));
Actions act = new Actions(driver);
act.moveToElement(element).perform();