Selenium上机实验

一. 实验要求

1. 使用SeleniumIDE录制脚本和导出脚本

2. 编写Selenium Java WebDriver程序,测试input.xlsx表格中的学号和git地址的对应关系是否正确

二. 环境配置

1. Google Chrome 65.0、Chromedriver 2.37、Selenium IDE 3.0.1

2. Eclipse 4.72、poi-3.17.jar、poi-ooxml-3.17.jar、poi-ooxml-schemas-3.17.jar、selenium-java-2.45.0.jar等

三. 实验过程

1. 导入相关jar包

2. 编写代码

package lab2;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;


public class Main {
    public static void main(String[] args) throws Exception {
        //浏览器
        System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); //chromedriver服务地址
        WebDriver driver =new ChromeDriver(); 
        driver.get("https://psych.liebes.top/st"); //打开指定的网站
        //文档
        InputStream is = new FileInputStream("C:\\Users\\input.xlsx");
        Workbook wb = new XSSFWorkbook(is);
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();        
        Sheet sheet = wb.getSheetAt(0); //获取第一个sheet
        Row row = sheet.getRow(0); //获取第一行
        
        String name = null;
        String path = null;
        Map<String,String> map = new LinkedHashMap<String,String>();
        for(int i = 0; i < 96; i++) {
            row = sheet.getRow(i);
            if(row != null){
                name = (String) getCellFormatValue(row.getCell(0));
                path = (String) getCellFormatValue(row.getCell(1));
                map.put(name, path);        
            }else{
                break;
            }
            list.add(map);
        }
        
        for (Entry<String, String> entry : map.entrySet()) {
            String username = entry.getKey();
            String link = entry.getValue();
            driver.findElement(By.id("username")).sendKeys(username); 
            driver.findElement(By.id("password")).sendKeys(username.substring(4,10)); 
            driver.findElement(By.id("submitButton")).click();
            
            String address = driver.findElement(By.xpath("//p")).getText();
            if(address.equals(link)) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
            
            try {
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);      
                driver.get("https://psych.liebes.top/st");
            } catch (Exception e) {
                e.printStackTrace();
            }    
        }
        
        driver.close(); //退出浏览器
    }
    
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //判断cell是否为日期格式
                if(DateUtil.isCellDateFormatted(cell)){
                    //转换为日期格式YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                }else{
                    //数字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }
}

四. 实验结果

转载于:https://www.cnblogs.com/silver-nitrate/p/8847168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值