selenium + httpclient 邮箱模拟登录

1、selenium版本

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.seleniumgroupId>
            <artifactId>selenium-javaartifactId>
            <version>2.45.0version>
        dependency>
        <dependency>
            <groupId>net.sourceforge.htmlunitgroupId>
            <artifactId>htmlunitartifactId>
            <version>2.15version>
        dependency>
    dependencies>

2、核心代码:

System.setProperty("webdriver.chrome.driver", "E:/BaiduYunDownload/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
//      WebDriver driver = new HtmlUnitDriver();
        driver.get("https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=522005705&daid=4&s_url=https://mail.qq.com/cgi-bin/login?vt=passport%26vm=wpt%26ft=loginpage%26target=&style=25");

        driver.findElement(By.cssSelector("#switcher_plogin")).click();

        driver.findElement(By.cssSelector("#u")).clear();
        driver.findElement(By.cssSelector("#u")).sendKeys("XXXX");
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#p")).clear();
        driver.findElement(By.cssSelector("#p")).sendKeys("XXXX");

        driver.findElement(By.cssSelector("#login_button")).click();

        Thread.sleep(1000);
//      driver.get("https://mail.qq.com/cgi-bin/frame_html");

        //      
        Set cookieSet = driver.manage().getCookies();
//System.out.println(driver.manage().getCookies().toString());
        String cookieStr = "";
        for (Cookie cookie : cookieSet) {
            cookieStr += cookie.getName() + "=" + cookie.getValue() + "; ";
        }


        System.out.println(cookieStr);
//      System.out.println(driver.getPageSource());
        HttpCrawl httpCrawl = new HttpCrawl();
        Map headers = new HashMap();
        headers.put("Cookie", cookieStr);
        String html = httpCrawl.getMethodResult("https://mail.qq.com/cgi-bin/login?vt=passport&vm=wsk&delegate_url=", null, headers);

        System.out.println(html);
        driver.close();
        System.exit(0);

你可能感兴趣的:(selenium + httpclient 邮箱模拟登录)