selenium + jsoup 登陆网页

来源:互联网 发布:李擂 知乎 编辑:程序博客网 时间:2024/04/30 14:50
//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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肾癌手术后发烧怎么办 尿结石堵住尿道怎么办 尿结石不能排尿怎么办 肾癌小便有血怎么办 膀胱癌膀胱全切怎么办 怀孕了有阑尾炎怎么办 食物堵塞在食管怎么办 食物卡在食管怎么办 小孩食道卡异物怎么办 八十岁老人得了膀胱癌怎么办 肾结石引起吐血尿血怎么办 肾结石引起的尿血怎么办 食道感觉有异物怎么办 膀胱出血有血块怎么办 肾小球滤过率20怎么办 膀胱癌术后有血尿怎么办 肾病贫血怎么办吃什么 低蛋白血症怎么办 慢性肾炎患者感冒了怎么办 透析病人磷高怎么办 尿毒症透析磷高怎么办 怀孕了有膀胱炎怎么办 宝宝拉肚子尿少怎么办 猫尿血怎么办吃什么药 肝癌小便不出来怎么办 怀孕三个月结石血尿怎么办? 肾小球滤过率65怎么办 肾穿后有血肿6cm怎么办 肾穿刺后血肿怎么办 手术后有血肿怎么办 尿蛋白胆红素高怎么办 哺乳期尿蛋白高怎么办 产后老放屁便秘怎么办 肝癌移植后复发怎么办 产后屁多便秘怎么办 肝癌切除后复发怎么办 宝宝便秘拉不出大便怎么办 宝宝便秘屁多怎么办 尿液浑浊气味重怎么办 经常便秘肚子胀怎么办 顺产满月后腰疼怎么办