Xpath 不适用于 Selenium 中的 MouseOver(CatMenu)

Xpath is not working on MouseOver(CatMenu) in Selenium

关于无法打开鼠标悬停的所有网络驱动程序的硒问题menu.Altough assess to Xpath of element 网络驱动程序无法打开鼠标悬停(CatMenu)并且日志是"Must provide a location for a move action"。

我想转到 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();

}

您可以尝试使用 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();

使用下面的代码实现同样的效果-

    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 子菜单