如何使用selenium获取所有链接(标题和子链接)

时间:2016-09-02 06:45:05

标签: css arrays selenium xpath

实际上我正在检查excel值是否显示在网页鼠标悬停菜单上。菜单包括标题和菜单项链接。主菜单和子链接中的菜单项位于子菜单中。当我试图收集所有链接时,我通过CSS找到它们。我想在一个数组中获取所有链接。我使用了以下命令。请帮我如何获取一个数组中的所有元素。开发人员代码附加为屏幕截图:

hovermenu

hovermenucodefortitle

hovermenucodefortitle

String AdminRoleAccessfilepath = "D:\\PMC  
Automation\\AdminRoleAccess.xls";
FileInputStream AdminRoleFile=new 
FileInputStream(AdminRoleAccessfilepath);
Workbook rwb=Workbook.getWorkbook(AdminRoleFile);
Sheet AdminRolesheet=rwb.getSheet("AdminRole");
int submenucolumnsize=AdminRolesheet.getRows();
int menucolumnsize=AdminRolesheet.getRows();
String[] arraysubmenu=new String[submenucolumnsize];
String[] arraymenu=new String[menucolumnsize];

List<WebElement>SubTitlelist = 
driver.findElements(By.cssSelector(".child_menu>a "));
List<String> allSubTitleslist=new ArrayList<>();
List<WebElement>Titleslist = 
driver.findElements(By.cssSelector("#menu_nav>ul>li>a "));

int submenuui = 0;
for (int a=1;a<submenucolumnsize;a++)
{
    arraysubmenu[a] = AdminRolesheet.getCell(1,a).getContents();
    //System.out.println(arraymenu[a]);

        boolean isthere = true;

        for(submenuui=0; submenuui<SubTitlelist.size(); submenuui++)
        {
        if 
(!(arraysubmenu[a]).equalsIgnoreCase
(SubTitlelist.get(submenuui).getText()))
        {
            isthere = false;
      }
        else
        {
            isthere = true;
            break;
        }
      }
if(isthere)
        System.out.println("\nThe Sub Menu item in the UI-> " 
+arraysubmenu[a]+ " existing on the Web Page" );
    else
        System.out.println("\nThe Sub Menu item in the UI-> " 
+arraysubmenu[a]+ " not existing on the Web Page");
}

int menuui = 0;
    for (int b=1;b<menucolumnsize;b++)
    {
        arraymenu[b] = AdminRolesheet.getCell(1,b).getContents();
        //System.out.println(arraymenu[a]);
            boolean isthere = true;

            for(menuui=0; menuui<Titleslist.size(); menuui++)
            {   
            if (!(arraymenu[b]).equalsIgnoreCase
 (Titleslist.get(menuui).getText()))
            {
                isthere = false;
             }
            else
            {
                isthere = true;
                break;
            }
           }
 if(isthere)
            System.out.println("\nThe Title in the Menu in the UI-> " 
 +arraymenu[b]+ " existing on the Web Page" );
        else
            System.out.println("\nThe Title in the Menu in the UI-> " 
 +arraymenu[b]+ " not existing on the Web Page");

    }

1 个答案:

答案 0 :(得分:0)

使用逗号将选择器组合成一个CSS。

'.child_menu>a , #menu_nav>ul>li>a'