Selenium webdriver Selenium webdriver-无法单击子菜单-amazon.in

Selenium webdriver Selenium webdriver-无法单击子菜单-amazon.in,selenium-webdriver,click,submenu,Selenium Webdriver,Click,Submenu,我正试图点击亚马逊网站中的submneu 我可以展开主菜单。但不能点击子菜单。我尝试了两种方法。但它显示出错误 邮件类别html代码: <li class="nav_pop_li nav_cat nav_divider_before" id="nav_cat_2">Books</li> 方法2 driver.get("http://amazon.in"); driver.manage().window().maximize(); //locate

我正试图点击亚马逊网站中的submneu 我可以展开主菜单。但不能点击子菜单。我尝试了两种方法。但它显示出错误

邮件类别html代码:

<li class="nav_pop_li nav_cat nav_divider_before" id="nav_cat_2">Books</li>
方法2

driver.get("http://amazon.in");
        driver.manage().window().maximize();
    //locate the menu to hover over using its xpath
            WebElement menu = driver.findElement(By.xpath("//li[@id='nav_cat_2']"));

            //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. This allows sub menu appears properly before trying to click on it
            WebDriverWait wait = new WebDriverWait(driver, 5); 
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@class='nav_a nav_item']")));  // until this submenu is found

            //identify menu option from the resulting menu display and click
            WebElement menuOption = driver.findElement(By.xpath("//a[@class='nav_a nav_item']"));
            menuOption.click();
请告诉我为什么它不工作?

方法1中,我想在悬停时出现了问题。我必须说,它只是点击了“主要类别”元素

方法2“中,“主类别”的悬停正确发生。唯一的问题是选择点击“子类别”的xpath。xpath实际上与多个元素相关联。因此,代码失败了

只需更换方法2中的以下代码,即可工作:-

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@class='nav_a nav_item' and .=\"All Books\"]")));

注意:-在上面,我使用了所有书籍作为要单击的文本
此文本必须与网页中显示的内容完全匹配。
类似地,您可以用其他文本(如“畅销书、新版本和预购书等”)替换所有书籍

driver.get("http://amazon.in");
        driver.manage().window().maximize();
    //locate the menu to hover over using its xpath
            WebElement menu = driver.findElement(By.xpath("//li[@id='nav_cat_2']"));

            //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. This allows sub menu appears properly before trying to click on it
            WebDriverWait wait = new WebDriverWait(driver, 5); 
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@class='nav_a nav_item']")));  // until this submenu is found

            //identify menu option from the resulting menu display and click
            WebElement menuOption = driver.findElement(By.xpath("//a[@class='nav_a nav_item']"));
            menuOption.click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@class='nav_a nav_item' and .=\"All Books\"]")));
WebElement menuOption = driver.findElement(By.xpath("//a[@class='nav_a nav_item' and .=\"All Books\"]"));