Use Selenium to save cookie after log on website

原创 2016年08月30日 13:59:39


#!/usr/bin/env python


#coding=utf-8

import urllib2
import urllib
import cookielib

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("https://abc.com/")

time.sleep(3)
driver.maximize_window()

driver.find_element_by_id("user").send_keys("xxxx")
driver.find_element_by_id("password").send_keys("xxxx")

time.sleep(3)
driver.find_element_by_id("submit").click()

#get the session cookie  
cookie = [item["name"] + "=" + item["value"] for item in driver.get_cookies()]  
print cookie  
      
cookiestr = ';'.join(item for item in cookie)  
print cookiestr  

print '%%%using the urllib2 !!'  
homeurl=driver.current_url
homeurl = "https://abc.com/index.php/apps/ws/products"
print 'homeurl: %s' % homeurl  
headers = {'cookie':cookiestr}  
req = urllib2.Request(homeurl, headers = headers)  
try:  
    response = urllib2.urlopen(req)  
    text = response.read()  
    fd = open('homepage.html', 'w')  
    fd.write(text)  
    fd.close()  
    print '###get home page html success!!'  
except:  
    print '### get home page html error!!'  

time.sleep(5)
driver.close()

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Re: How to use values of properties file in selenium code - msg#02103

Great points Mark, thanks. That will be a good reasoning of disadvantages to the approach. Also w...
  • aerchi
  • aerchi
  • 2012-10-18 10:39
  • 1856

How to restore ASM based OCR after complete loss of the CRS diskgroup on Linux/Unix systems [ID 1062

How to restore ASM based OCR after complete loss of the CRS diskgroup on Linux/Unix systems [ID 1062...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Use Python to Drive Selenium RC(转)

转自:http://www.cnblogs.com/oscarxie/archive/2008/07/20/1247004.html Selenium RC支持多种编程语言驱动客户端浏览器,...

How to use the System Restore API to save and to restore system data in Visual C++

System Restore API to save and to restore system data

MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk

转载自:http://www.redicecn.com/html/Linux/20131125/468.html Redis "MISCONF Redis is configured to save ...

Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on di

今天第二次遇到Redis “MISCONF Redis is configured to save RDB snapshots, but is currently not able to persis...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)