cookie登录
登录后获取cookie语句
- cookies = browser.get_cookies()
- # print(cookies)
打印出来的cookie是一大串,手动整理后使用
- c1 = {'domain': '.chinapmp.cn',
- 'httpOnly': True,
- 'name': 'user',
- 'path': '/',
- 'secure': False,
- 'value': '8rqt9h24a0WiRpkBXo9Bm_9i_yb_Yv8W'}
- c2= {'domain': '.chinapmp.cn',
- 'httpOnly': True,
- 'name': 'SYSTEM_TEMP_COOKIE_NAME',
- 'path': '/', 'secure': False,
- 'value': '6AC1A8CD62E9B666F3A9C0629CF54A77'}
- c3= {'domain': '.chinapmp.cn',
- 'expiry': 1634698949,
- 'httpOnly': True,
- 'name': 'SYSTEM_ONLY_COOKIE_NAME',
- 'path': '/',
- 'secure': False,
- 'value': '8E446DBDE647A599F88988C7F9401BF4'}
- browser.add_cookie(c1)
- browser.add_cookie(c2)
- browser.add_cookie(c3)
add_cookie里每一个键值对的意义:
- name:cookie的名称
- value:cookie对应的值,动态生成的
- domain:服务器域名
- expiry:Cookie有效终止日期
- path:Path属性定义了Web服务器上哪些路径下的页面可获取服务器设置的Cookie
- httpOnly:防脚本攻击
- secure:在Cookie中标记该变量,表明只有当浏览器和Web Server之间的通信协议为加密认证协议时
接下来可以直接在获取需要登录的网页
- browser = webdriver.Chrome()
- url = "http://exam.chinapmp.cn/" # 这个是根链接
- url1= "http://user.chinapmp.cn/examsign;sign.shtml" #这个是需要登录后才能访问的其他链接
- browser.get(url1)
token登录
有的网页不用cookies,有自己的token,没关系,继续试试token
- #获取token
- token=browser.execute_script('window.localStorage.getItem("token")')
- print(token)
- #添加token
- js='window.localStorage.setItem("token","token值")'
- browser.execute_script(js)
- browser.refresh()#刷新
全部脚本
- import logging
- import os
- import platform
- import time
-
- import selenium.webdriver.support.ui as ui
- from selenium import webdriver
- from selenium.common.exceptions import NoSuchElementException, TimeoutException
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.support.wait import WebDriverWait as WD
- from selenium.webdriver.chrome.options import Options
-
- # from time import sleep, time
- # 首页界面
- login_user = "Login_uName" # id
- login_password = "Login_uPass" # id
- login_button = "uLogin" # id
- sign_entrance = "/html/body/div[3]/div[1]/div[2]/div[1]/div/ul/li[1]/span/a" # xpath
- i_sign_entrance = "/html/body/div[2]/div[1]/div[1]/div[2]/table/tbody/tr[1]/td/div/div[2]/div[1]/div/a" # xpath
- next_button = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody/tr/td[2]/div/a"
- yes_button ="clause_yes"
- info_confirm ="/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[21]/td/input"
- exam_type_1 = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[2]/td[1]/span/label[1]/input"
- exam_type_2 = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[2]/td[1]/span/label[2]/input"
- last_name ="Xing"
- middle_name ="Zhong"
- first_name = "Ming"
- training_agency = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[6]/td[1]/select/option[2]"
- username_web = "PMIUname"
- password_web = "PMIUpass"
- dateF = "PMItimeB"
- dateF1="/html/body/div[4]/table/tbody/tr[2]/td[1]/a"
- dateT = "PMItimeE"
- dateT1 ="/html/body/div[4]/table/tbody/tr[5]/td[1]/a"
- test_site = "//*[@id='Kaodian']/option[7]" # 大连东软
- finish = "//*[@id='vform']/table/tbody/tr[14]/td/input"
-
- logging.basicConfig(
- level=logging.WARNING,
- format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
- )
-
- # 无头模式打开浏览器
- __options = Options()
- __options.add_argument('--headless')
- __options.add_argument('--window-size=1440x900')
- __options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
- # 无头模式打开浏览器
- #browser = webdriver.Chrome(chrome_options=__options)
- # 不使用无头模式打开浏览器
- browser = webdriver.Chrome()
-
- url = "http://exam.chinapmp.cn/"
- url1= "http://user.chinapmp.cn/examsign;sign.shtml"
-
- browser.get(url)
-
- wait2s = ui.WebDriverWait(browser, 2)
- wait10s = ui.WebDriverWait(browser, 10)
- wait30s = ui.WebDriverWait(browser, 30)
-
- try:
- # 登录
-
- browser.get(url)
- wait30s.until(EC.title_is('首页-项目管理职业资格认证'))
- #BasePage = Page.BasePage
- element = browser.find_element_by_xpath(sign_entrance)
- element.click()
- # BasePage.click("xpath",sign_entrance)
- #wait30s.until(EC.title_is('会员登录-首页-项目管理职业资格认证'))
- time.sleep(1)
- username = browser.find_element_by_id(login_user)
- username.send_keys("chai1005") # 请再这儿输入用户名!
- password = browser.find_element_by_id(login_password)
- time.sleep(1)
- password.send_keys("chai1005") # 请再这儿输入密码!
- browser.find_element_by_id(login_button).click()
- # password.send_keys(Keys.ENTER)
- time.sleep(1)
- cookies = browser.get_cookies()
- print(cookies)
- c1= cookies[0]
- print("c1: ",c1)
- c2 = cookies[1]
- print("c2: ", c2)
- c3 = cookies[2]
- print("c3: ", c3)
- # 第一次的时候需要获取cookie,第一次以后可以手动保存起来,一段时间内是有效的,保存为下面格式即可
- # c1 = {'domain': '.chinapmp.cn','httpOnly': True,'name': 'user','path': '/','secure': False,'value': 'Te6Im1OPs5FPP88z0Ixj2f9i_yb_Yv8W'}
- # c2= {'domain': '.chinapmp.cn','httpOnly': True,'name': 'SYSTEM_TEMP_COOKIE_NAME','path': '/', 'secure': False,'value': 'B3D8E21B1D65DA723114967F114E359D'}
- # c3= {'domain': '.chinapmp.cn','expiry': 1634708549, #1634698949'httpOnly': True,'name': 'SYSTEM_ONLY_COOKIE_NAME','path': '/','secure': False,'value': 'B5B26CE39ED2C81F82FDF17D1FC2C4E5'}
- browser.add_cookie(c1)
- browser.add_cookie(c2)
- browser.add_cookie(c3)
- browser.get(url1)
- # 点击报名
- # browser.find_element_by_xpath(i_sign_entrance).click()
- # time.sleep(1)
- # 点击下一步
- # browser.find_element_by_xpath(next_button).click()
- # time.sleep(61)
- # 点击同意
- # browser.find_element_by_id(yes_button).click()
- # time.sleep(1)
- # 点击个人信息确认无误
- # browser.find_element_by_xpath(info_confirm).click()
- # time.sleep(1)
- # 输入报考信息
- browser.find_element_by_xpath(exam_type_1).click()
- browser.find_element_by_id(last_name).send_keys("XXXX")
- browser.find_element_by_id(middle_name).send_keys("XXXX")
- browser.find_element_by_id(first_name).send_keys("XXXX")
- browser.find_element_by_xpath(training_agency).click()
- browser.find_element_by_id(username_web).send_keys("XXXX")
- browser.find_element_by_id(password_web).send_keys("XXXX")
- #browser.find_element_by_id(dateT).send_keys(Keys.ENTER)
- browser.find_element_by_id(dateF).click()
- browser.find_element_by_xpath(dateF1).click()
- #browser.find_element_by_id(dateF).send_keys(Keys.ENTER)
- browser.find_element_by_id(dateT).click()
- browser.find_element_by_xpath(dateT1).click()
- browser.find_element_by_xpath(test_site).click()
- browser.save_screenshot("./filename.png")
- # 点击报名
- #browser.find_element_by_xpath(finish).click()
-
- time.sleep(10)
- finally:
- browser.close()