Use Selenium to save cookie after log on website


#!/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()

阅读更多
个人分类: Selenium Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Use Selenium to save cookie after log on website

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭