python處理web網頁cookies實現自動登入

zhusongziye發表於2017-09-06

廢話少說,直接上程式碼。嘎嘎。。。

# coding=utf-8
from selenium import webdriver
import time

# 手動登入百度並勾選“下次免登陸”,再用程式碼載入配置檔案開啟百度即可實現自動登入。
# 使用程式碼自動登入百度,退出後,程式碼再載入配置檔案是不能自動登入的,必須手動登入一次並勾選上“下次免登陸”

# 配置檔案地址
profile_directory = r'C:\Users\Dell\AppData\Roaming\Mozilla\Firefox\Profiles\8nq06rq1.default'
# 載入配置配置
profile = webdriver.FirefoxProfile(profile_directory)
# 啟動瀏覽器配置
drive = webdriver.Firefox(profile)

# drive = webdriver.Firefox()
drive.get("http://www.baidu.com")
aa = drive.get_cookies()
print(aa)
time.sleep(5)

drive.find_element_by_xpath(".//*[@id='u1']/a[7]").click()
time.sleep(3)
print(11)
url = drive.current_window_handle
drive.switch_to.window(url)
time.sleep(3)
drive.find_element_by_name("userName").clear()
drive.find_element_by_name("userName").send_keys("xxxxx@163.com")
drive.find_element_by_name("password").clear()
drive.find_element_by_name("password").send_keys("xxxxx")
drive.find_element_by_name("memberPass").click()
time.sleep(15)
drive.find_element_by_id("TANGRAM__PSP_8__submit").click()

bb = drive.get_cookies()
print(bb)


# drive.find_element_by_id("kw").send_keys("selenium2")
# drive.find_element_by_id("su").click()
time.sleep(10)
drive.quit()


相關文章