Selenium WebDriver 自动化测试 第二章

1.处理下拉框

Select select = new Select(dr.findElement(By.id("User_Age")));

select.selectByIndex(1);   //根据index选择

select.selectByValue("终端");  //根据value选择

select.selectByVisibleText("机器人");  //根据visibletext选择

 //遍历一下下拉列表所有选项,用click进行选中选项
  for(WebElement e : select.getOptions())
   e.click();
 }

2.处理表格

  1)先写一个返回单元格元素的方法

    public WebElement getElement(By by, String tableCellAddress) {
        //得到table元素对象
        WebElement table = tableDriver.findElement(by);
        //对所要查找的单元格位置字符串进行分解,得到对应行、列。
        int index = tableCellAddress.trim().indexOf('.');
        int row = Integer.parseInt(tableCellAddress.substring(0, index));
        int cell = Integer.parseInt(tableCellAddress.substring(index+1));
        //得到table表中所有行对象,并得到所要查询的行对象
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        WebElement theRow = rows.get(row);
        WebElement rowcell = getCell(theRow, cell);
        //调用getCell方法得到对应的列对象,然后得到要查询的文本。
        //String text = getCell(theRow, cell).getText();
        return rowcell;
    }
    
    private WebElement getCell(WebElement Row,int cell) {
        List<WebElement> cells;
        WebElement target = null;
        //列里有"<th>"、"<td>"两种标签,所以分开处理。
        if(Row.findElements(By.tagName("th")).size()>0) {
            cells = Row.findElements(By.tagName("th"));
            target = cells.get(cell);
        }
        if(Row.findElements(By.tagName("td")).size()>0) {
            cells = Row.findElements(By.tagName("td"));
            target = cells.get(cell);
        }
        return target;
    }

  2)调用此方法

By by = By.className("items");
Table table = new Table(driver);
WebElement operation = table.getElement(by, "1.9");

目前接触的就这么多,操作起来也不怎么复杂

原文地址:https://www.cnblogs.com/mayibanjiah/p/4211554.html