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()

相关文章推荐

Use photoshop to design website

  • 2009年10月15日 19:22
  • 2.82MB
  • 下载

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...

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
  • 1916

Use Python to Drive Selenium RC(转)

转自:http://www.cnblogs.com/oscarxie/archive/2008/07/20/1247004.html Selenium RC支持多种编程语言驱动客户端浏览器,...
  • wsywfw3
  • wsywfw3
  • 2011年08月08日 18:39
  • 685

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 ...

Note On <Programming Entity Framework 2nd Edition> -01: How to Use LINQPad 4.5

如何安装第二章的数据库 去到本书官网的下载页面: 下载完成后执行压缩包里的SQLPackageSampleDatabaseChapters2thru6.exe文件,运行结果如下: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Use Selenium to save cookie after log on website
举报原因:
原因补充:

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