Selenium初步应用

    /**
     * 添加工人自动测试化脚本
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL4\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://192.168.10.203:9529/#/login?redirect=%2F");
        //定位对象时给 10s 的时间, 如果 10s 内还定位不到则抛出异常
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        String title = driver.getTitle();
        System.out.println("title:" + title);
        driver.findElementByName("username").sendKeys("13425484274");
        driver.findElementByName("password").sendKeys("123456");
        driver.findElementByXPath("//button[@class='el-button el-button--primary el-button--default']").click();
//        String currentUrl = driver.getCurrentUrl();
//        String driverTitle = driver.getTitle();
//        String source = driver.getPageSource();
//        System.out.println("当前网址:" + currentUrl);
//        System.out.println("标题:" + driverTitle);
//        System.out.println("来源:" + source);
        //颐和劳务
        driver.findElementById("tab-name_29").click();
        //driver.findElementByXPath("//img[@title='添加' and @class='leficon']").click();
        //读取execl数据
        FileInputStream fileInput = new FileInputStream("C:\\Users\\DELL4\\Desktop\\567.xlsx");//创建文件输入流
        XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象
        XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
        int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
        //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
        for (int i = 1; i <= lastRowNum; ++i) {
            driver.findElementByXPath("//img[@title='添加' and @class='leficon']").click();
            XSSFRow row = sheet.getRow(i);//获取每一行
            int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
            for (int j = 1; j < columnNum; ++j) {
                XSSFCell cell = row.getCell(j);//获取每个单元格
                if (j == 4) {
                    driver.findElementByXPath("//input[@placeholder='请输入薪资']").sendKeys(cell.getRawValue());
                } else if (j == 5) {
                    driver.findElementByXPath("//input[@placeholder='加班费']").sendKeys(cell.getRawValue());
                } else if (j == 1) {
                    Thread.sleep(1000);
                    driver.findElementByXPath("//input[@placeholder='请输入姓名']").sendKeys(cell.getStringCellValue());
                } else if (j == 2) {
                    driver.findElementByXPath("//input[@placeholder='请输入身份证号']").sendKeys(cell.getStringCellValue());
                } else if (j == 3) {
                    driver.findElementByXPath("//input[@placeholder='请选择工种']").click();
                    //请选择下拉框
                    driver.findElementByXPath("//span[text()='" + cell.getStringCellValue() + "']").click();
                }
            }
            //添加
            driver.findElementByXPath("//button[@class='el-button el-button--primary el-button--medium']").click();
        }
        wb.close();
        fileInput.close();
        driver.quit();
    }
posted @ 2021-05-27 15:05  白描先生  阅读(49)  评论(0编辑  收藏  举报
}); });