Selenium Get microblogging cookies

Source: Internet
Author: User
Tags xpath

I've always heard that selenium decided to try it today. Try to get a cookie for the microblogging first.

Install Selenium First

Pip Install Selenium

Then download the browser driver, here I use the Google's
Https://sites.google.com/a/chromium.org/chromedriver/downloads
Download the latest words, their own Google browser also if the latest. Click on the browser help-> about Google Chrome
Update Now the latest version is 58

then put the downloaded driver in the root directory of Python

Then visit the microblogging login interface

Right-click to get the name of the user name and password element
Use XPath to get the login button (checked element-> check-> right button->copy->xpath)

#-*-Coding:utf-8-*-from
Selenium import webdriver
#调用 chrom browser
import time
driver = Webdriver. Chrome ()
#访问微博登录界面
driver.get (' http://weibo.com/login.php ')
#获取用户名和密码的输入框
loginname = Driver.find_element_by_name (' username ')

password = driver.find_element_by_name (' password ')
# Use XPath to get login button
btn = Driver.find_element_by_xpath ('//*[@id = ' pl_login_form ']/div/div[3]/div[6]/a ')
# Set the value of the two input boxes
loginname.send_keys (' 183xxxxxxxxx ')
password.send_keys (' xxxxxxxxxx ')
#让登录按钮产生点击事件
Btn.click ()
#获取Cookie
cookies = driver.get_cookies ()
#获取到Cookie后进行拼接 for later use
Cookie_ List =[] for
i in cookies:
    cookie =i[' name ']+ ' = ' +i[' value ']
    cookie_list.append (cookie)
Cookie_ str = '; '. Join (cookie_list)
print cookie_str
js_code= ' alert (document.cookie) '
driver.execute_script (js_ Code

Get the element can be obtained according to Id,name,classname,xpath, very convenient.
Enter values with Send_keys
Gets the object that can control its events.
Execute the JS statement can be Driver.execute_script (Js_code)

will continue to study later ~

Reference:
http://mp.weixin.qq.com/s?__biz=MjM5MDEyMDk4Mw==&mid=2650166459&idx=2&sn= 70975e176bda5d9adeb4e4b218b23c52&chksm= be4b59c3893cd0d5a3aad2994298afe1de7dfdb682d2a7cc61c3e3494d27b2ea6a0d6d6e7552&mpshare=1&scene=23& Srcid=0427rvxy59k7sb6lyssod3mg#rd

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.