Selenium to simulate login, get login Cookies__selenium

Source: Internet
Author: User
background

The crawl order requires the logged in page data.
The resource in which the operation requires logon permissions. Experimental Environment windows 7 firefox_v50.1.0 geckodriver_v0.14 Introduction

Selenium Webdriver is an automated test tool for Web applications.
Has a series of APIs to manipulate the browser.
You can simulate user action page elements. main step configuration parameters, initialize driver load URL Wait page load get page Element action page element submit form get cookies Save cookies Note on the old version, you will set the Firefox path, but this The version only needs to set the gecko path. Gecko need to be downloaded from git. Because it is to open the browser to load the page, so need some delay to wait for the page to load. You can set the time, or you can set the detection of an element presence. Cookies are stored in the Org.apache.http.client.CookieStore and then serialized into a file for the next read, with HttpClient to catch the data. The dependency required

Join in the dependencies of Pom.xml

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId> fluent-hc</artifactid>
        <version>4.5.2</version>
    </dependency>
    < dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId> selenium-firefox-driver</artifactid>
        <version>3.0.1</version>
    </dependency>
example code for login Csdn
Package Firefox;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.ObjectOutputStream;
Import Java.util.Iterator;
Import Java.util.Set;

Import Java.util.concurrent.TimeUnit;
Import Org.apache.http.client.CookieStore;
Import Org.apache.http.impl.client.BasicCookieStore;
Import Org.apache.http.impl.cookie.BasicClientCookie;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.Cookie;
Import org.openqa.selenium.WebElement;

Import Org.openqa.selenium.firefox.FirefoxDriver;
     public class Main {/** * analog csdn login, get login cookies Save to File * * @param username * username * @param password * password * @param geckodriverpath * Gecko Path (see Https://github.com/mozill A/geckodriver) * @param savecookiepath * Cookies saved path * @throws Exception/Public STA tic void Firfoxdrivergetcookies (string username, string password, string geckodriverpath, String Savecookiepat h) throws EXception {//initialization parameters according to System.setproperty ("Webdriver.gecko.driver", Geckodriverpath);
        Firefoxdriver Driver = new Firefoxdriver ();
        String BaseURL = "Https://passport.csdn.net/account/login?ref=toolbar";
        Load URL driver.get (baseurl);
        Wait for the load to complete driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
        Gets the page element webelement elemusername = driver.findelement (By.name ("username"));
        Webelement Elempassword = driver.findelement (by.name ("password"));
        Webelement btn = driver.findelement (By.classname ("Logging"));
        Webelement rememberme = driver.findelement (By.id ("RememberMe"));
        Operation page element Elemusername.sendkeys (username);
        Elempassword.sendkeys (password);
        Rememberme.click ();
        Submit the form btn.submit ();
        Thread.Sleep (5000);
        Driver.get ("http://msg.csdn.net/");
        Thread.Sleep (5000); Get Cookies Driver.manage (). GetCookieS ();
        set<org.openqa.selenium.cookie> cookies = Driver.manage (). GetCookies ();
        System.out.println ("Size:" + cookies.size ());

        iterator<org.openqa.selenium.cookie> ITR = Cookies.iterator ();

        Cookiestore Cookiestore = new Basiccookiestore ();
            while (Itr.hasnext ()) {Cookie cookie = Itr.next ();
            Basicclientcookie Bcco = new Basicclientcookie (Cookie.getname (), Cookie.getvalue ());
            Bcco.setdomain (Cookie.getdomain ());
            Bcco.setpath (Cookie.getpath ());
        Cookiestore.addcookie (Bcco); //Save to file ObjectOutputStream Oos = new ObjectOutputStream (new FileOutputStream (new file (Savecookiepath))
        ;
        Oos.writeobject (Cookiestore);

    Oos.close (); public static void Main (string[] args) throws Exception {firfoxdrivergetcookies ("< username >", "< password"
    ; "," C:/bin/geckodriver.exe "," Cookie.ser ");
 }
}
ReferenceWebdriver Chinese Community automated test for WEB applications based on Selenium webdriver Mozilla/geckodriver Selenium webdriver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.