selenium + jsoup 登陆网页

//jsoup获取cookies
private static Integer TIMEOUT = 10000;
    private static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
    private static String URL="http://www.***.com/loginnew.asp";
    public static Map getLoginCookeis(){
         try {
//登陆时发送的数据,
            Map map = new HashMap();
            map.put("username", "用户名");
            map.put("password", "密码");
            map.put("Action", "Login");
            map.put("Submit.x", "40");
            map.put("Submit.y", "15");
            Map map1 = new HashMap();
            map1.put("ASPSESSIONIDASBTBDDT", "ACABMBFDKBGHOLHBHMKKMHLA");
            map1.put("Sailing", "Skin=");
            //发送请求
            Connection.Response  rs=Jsoup.connect(URL)
                    .postDataCharset("GB2312")//编码
                    .data(map)//请求参数
                    .userAgent(USER_AGENT)
                    .cookies(map1)//cookies
                    .timeout(TIMEOUT)//超时
                    .method(Connection.Method.POST)
                    .execute();
            map1=rs.cookies();//获取登录的cookies
            
            return map1;
        } catch (IOException ex) {
            Logger.getLogger(KechengbiaoLogin.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

//selenium登陆

public static void getIndex() {
        try {
//下载chromedriver.exe保存,使用chrome浏览器
            System.getProperties().setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
            WebDriver webDriver = new ChromeDriver();
//需先打开登录页面,然后将jsoup获取到的登录后的cookies放入页面
webDriver.get("http://www.****.com/loginnew.asp"); webDriver.manage().deleteAllCookies();



            Map cookies = KechengbiaoLogin.getLoginCookeis();
            for (Object entry : cookies.keySet()) {
                Cookie cookie = new Cookie(entry + "", cookies.get(entry) + "");
                webDriver.manage().addCookie(cookie);
            }

            webDriver.get("http://www.****.com/Index.asp");//登录后的页面
//左侧栏目菜单
            webDriver.switchTo().frame("left");//进入左侧frame,如果页面上有多个frame页面嵌套,这里就需要
//点开所有菜单
            List<WebElement> tbs = webDriver.findElements(By.xpath("//body/table"));
                for (WebElement we : tbs) {
                we.click();
              }

            List<WebElement> links = webDriver.findElements(By.tagName("a"));//获取左侧菜单所有链接
               webDriver.switchTo().defaultContent();//webDriver退到整个窗口,然后才能调转到别的frame里面去
                webDriver.switchTo().frame("frameName");
           } catch (InterruptedException ex) {
            Logger.getLogger(kechengbiaoIndex.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
             webDriver.close();//运行完需要关闭chrome driver,否则你的进程会无限增加
        }
    }



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值