【问题标题】:How to Print Submenu Links without any Attribute In Selenium如何在 Selenium 中打印没有任何属性的子菜单链接
【发布时间】:2018-07-06 14:47:56
【问题描述】:

我是 selenium 的新手,正在尝试使用 LIST 打印链接文本。

我正在尝试从一个演示站点获取文本,但定位器上没有任何单一属性。所以请帮帮我。以下是我执行的详细信息。

网站网址:https://www.nopcommerce.com/

菜单:产品 > 产品下的所有子菜单(共找到 9 个链接)但无法打印这 9 个链接的文本。

屏幕截图 1: enter image description here

屏幕截图 2: enter image description here

我做过的代码:

 public void OpenStoreDemo() {
    List<WebElement> MainMenu = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//a"));
    System.out.println(MainMenu.size());

    for (WebElement list : MainMenu) {

        String getname = list.getText();
        System.out.println(getname);

        if (getname.equals("Store demo")) {

            System.out.println("Pass");

        }
        else {
            System.out.println("Not Found");
        }
        break;
    }

}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    以下代码可能对您有所帮助。

    public void OpenStoreDemo() {
        boolean isFound = false;
        WebElement MainMenuProduct = driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a"));
    
        new Actions(driver).moveToElement(MainMenuProduct).perform();
    
        List<WebElement> lstProductSubmenu =driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//ul/li/a"));
    
        for (WebElement list : lstProductSubmenu) {
    
            String getname = list.getText();
            System.out.println(getname);
    
            if (getname.equals("Store demo")) {
    
                System.out.println("Pass");
                isFound = true;
                break;
            }
    
    
        }
        if(!isFound)
           System.out.println("not found");
    
    }
    

    【讨论】:

    • 在此行中出现错误“类型不匹配:无法从 WebElement 转换为 List”。 List lstProductSubmenu =driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]//ul/li/a"));
    • 对不起,是 findElements 不是 findElement。更新的答案。 List&lt;WebElement&gt; lstProductSubmenu =driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//ul/li/a"));
    • @Murthi 当我在 Eclipse 上运行这段代码时,它会打印出Not Found
    • 得到解决方案,删除 Else 部分并打破,它会工作。谢谢@Murthi
    • @Murthi,您可以尝试使用动态 XPath,因为如果开发人员在代码中添加任何 UL 或 LI,当前路径可能会中断。看到这个 //ul[@class='top-menu']/li[1]//ul//a
    【解决方案2】:

    试试这个:

    // to make 'Store demo' button visible, hover to 'product' button
    Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.xpath("//ul[@class = 'top-menu']/li[1]/a")));
    actions.perform();
    
    // wait until 'Store demo' button will be clickable
    new WebDriverWait(webDriver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class = 'top-menu']/li[1]/ul/li[4]/a")));
    // locate element
    WebElement storeDemoBtn = driver.findElement(By.xpath("//ul[@class = 'top-menu']/li[1]/ul/li[4]/a"));
    System.out.println(storeDemoBtn.getText()); // will print 'Store demo'
    

    【讨论】:

    • 这种xpath的目的是什么://*[@id='aspnetForm']/div[3]/div[2]/ul/li[1]/a跨度>
    • @cruisepandey 添加了一个较短的版本
    • 我在打印子菜单链接时遇到问题,等待不是解决方案。当我打印产品菜单下的文本时,每次打印空白而不是文本。我只需要打印 PRODUCT 菜单下的文本。
    【解决方案3】:

    您可以使用 JS 方法从隐藏的 webelement 中获取文本,并使用 dataprovider 方法检查是否启用了菜单项,可能是这样的:

     @DataProvider
    public Object[][] getProductMenu() {
        return new Object[][]{
                {"What is nopCommerce"},
                {"Why nopCommerce"},
                {"Features"},
                {"Store demo"},
                {"Showcase"},
                {"Case studies"},
                {"Roadmap"},
                {"Copyright notice removal"},
                {"License"}
        };
    }
    
    @Test(dataProvider = "getProductMenu")
    public void checkProductMenu(String menuItemName) {
        driver.get("https://www.nopcommerce.com/");
        // check if menu item is enabled
        Assert.assertTrue(isMenuItemExistInMenu(menuItemName));
    }
    
    public boolean isMenuItemExistInMenu(String menuItemName) {
        List<WebElement> menuItemsElements = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//ul/li/a"));
        for (WebElement menuItemsElement : menuItemsElements) {
            if (getHiddenTextByWebElement(menuItemsElement).equals(menuItemName)) {
                System.out.println("Menu item [" + menuItemName + "]" + " is enabled in Product submenu");
                return true;
            }
        }
        return false;
    }
    
    //this is JS method for get hidden text from WebElement
    private String getHiddenTextByWebElement(WebElement element) {
        try {
            return (String) driver.executeScript("return arguments[0].innerHTML", element);
        } catch (Exception e) {
            Assert.fail("Can't return hidden text, occur error: \n" + e.getMessage());
            return "";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多