selenium + httpclient 邮箱模拟登录

1、selenium版本

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.15</version>
        </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<Cookie> 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<String ,String > headers = new HashMap<String, String>();
        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);

你可能感兴趣的:(网络爬虫)