Use selenium to automatically log on to Taobao and get cookies

Source: Internet
Author: User
Tags seleniumhq

http://sunjun041640.blog.163.com/blog/static/256268322013101473549333/

Recently do a thing need to use the program to log Taobao and then go backstage to get something, analysis of the login page, found that Taobao useful flash to generate a string to limit the way we do not go through the browser and directly use the program login practices, then how to do it, I think I've seen Yunjie demonstrate a tool in the company that uses Python as a UI Automation test, and there should also be a Java interface. To find a, sure enough, it is selenium, very powerful, beyond imagination. Selenium can support a programmatic operation browser, including getting page content, manipulating elements, cookies, and so on, usually using the browser Plug-ins it provides to record UI Automation test scripts, and then running automated scripts to detect problems with our system functions, which saves a lot of manpower, And the procedure is not easy to make mistakes. And all I have to do is use it to help me login taobao, get cookies on it, and my program can get to login after the successful browser cookie, because now everyone's session is implemented with cookies. Selenium supports a variety of mainstream browsers, such as Ie,ff,chrome,opera, it also supports browser testing on Android, iOS, and also supports using programs on a machine to call browsers on B machines, which are not my focus.
First I use Firefox,java, according to the information on the Internet, do the following: 1, install the selenium IDE, that is, browser plug-in http://docs.seleniumhq.org/download/#side_plugins Page to find the corresponding plug-in address, download the installation, FF address is: HTTP://RELEASE.SELENIUMHQ.ORG/SELENIUM-IDE/2.4.0/SELENIUM-IDE-2.4.0.XPI 2, download selenium Server (formerly the Selenium RC server), which is a Java-written server that needs to be invoked when we call Selenium using client code, Download is a jar package: Selenium-server-standalone-2.37.0.jar starts it under the DOS command line: Java-jar Selenium-server-standalone-2.37.0.jar 3, write code, you can use many languages to call its interface: Java,python,c#,ruby,javascript and so I use Java, first download the Client interface package: Http://selenium.googlecode.com/files /selenium-java-2.37.0.zip

The following method gets the cookie that was successful after the login Taobao

public static string Click (string Username, string password) {Webdriver Webdriver = new Firefoxdriver (); webdriver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Webdriver.get ("http://login.1688.com/member/signin.htm");

Because Taobao login is actually embedded in an IFRAME inside, so first switch to the IFRAME and then operate Webdriver.switchto (). frame (0);

Enter the username webdriver.findelement (by.id ("Tpl_username_1")). Clear (); Webdriver.findelement (By.id ("Tpl_username_1")). SendKeys (username);

Enter the password webdriver.findelement (by.id ("Tpl_password_1")). Clear (); Webdriver.findelement (By.id ("Tpl_password_1")). SendKeys (password);

Click on the Login button webdriver.findelement (by.id ("j_submitstatic")). Click (); Webdriver.switchto (). Defaultcontent (); try {

Constant detection, once the current page URL is not the login page URL, it indicates that the browser has made a jump while (true) {thread.sleep (500L); if (!webdriver.getcurrenturl (). StartsWith ("http://login.1688.com/member/signin.htm")) {break;}} catch (Interruptedexception e) {e.printstacktrace ();}

Get the Cookie, jump out of the loop I think the login was successful, of course, the above judgment is not too strict, can be modified set<cookie> cookies = Webdriver.manage (). GetCookies (); String cookiestr = ""; for (Cookie cookie:cookies) {cookiestr + = Cookie.getname () + "=" + cookie.getvalue () + ";";}

Exit, Close browser webdriver.quit (); return cookiestr; Run, the above code runs, you will magically find that your FF automatically opened and automatically turned to the http://www.alimama.com/member/ Login.htm page, and the page a load complete, automatically entered the username, password, and clicked the Login button, the last login successful, the whole process is like someone in the manual operation, but in fact, the program operation. In this way, I got Taobao login after the successful cookie, now I want to use the program to access what resources can be.
After a while, I think the FF reaction is too slow, the chrome response is very fast, I switched to the chrome, but also the same as the above to operate: 1, install selenium IDE http:// Chromedriver.storage.googleapis.com/2.4/chromedriver_ Win32.zip, which has only one chromedriver.exe, put it into any directory inside the PATH environment variable in the operating system https://github.com/kyo-ago/ Selenium-ide-for-chrome download it down and then in Chrome's tool-> Extender-> load the Extender being developed, install into Chrome 2, start Selenium-server:java-jar Selenium-server-standalone-2.37.0.jar 3, coded Python code, I switched to Python again, feeling simple, fast development: first install SELENIUM:PIP install with PIP Selenium Write Python code again:

def get_cookie (username, password):

Browser = Webdriver. Chrome () url = "https://login.taobao.com/member/login.jhtml" browser.get (URL)

Go to the IFRAME to Browser.switch_to_frame (Browser.find_element_by_name ("Taobaologinifr"))

Enter the username browser.find_element_by_id ("Tpl_username_1"). Clear () browser.find_element_by_id ("Tpl_username_1"). Send_ Keys (username)

Enter the password browser.find_element_by_id ("Tpl_password_1"). Clear () browser.find_element_by_id ("Tpl_password_1"). Send_ Keys (password)

Click on the Login button browser.find_element_by_id ("J_submitstatic"). Click (); Browser.switch_to_default_content ()

Detect whether the URL has changed, I think the login success, simple point why while True:if Browser.current_url!= url:break; Time.sleep

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.