当前位置:   article > 正文

22_selenium_使用cookie直接登录

httponly true 怎么用selenium登录

参考:https://blog.csdn.net/sinat_34209942/article/details/81235925?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.edu_weight

cookie登录

登录后获取cookie语句

  1. cookies = browser.get_cookies()
  2. # print(cookies)

打印出来的cookie是一大串,手动整理后使用

  1. c1 = {'domain': '.chinapmp.cn',
  2. 'httpOnly': True,
  3. 'name': 'user',
  4. 'path': '/',
  5. 'secure': False,
  6. 'value': '8rqt9h24a0WiRpkBXo9Bm_9i_yb_Yv8W'}
  7. c2= {'domain': '.chinapmp.cn',
  8. 'httpOnly': True,
  9. 'name': 'SYSTEM_TEMP_COOKIE_NAME',
  10. 'path': '/', 'secure': False,
  11. 'value': '6AC1A8CD62E9B666F3A9C0629CF54A77'}
  12. c3= {'domain': '.chinapmp.cn',
  13. 'expiry': 1634698949,
  14. 'httpOnly': True,
  15. 'name': 'SYSTEM_ONLY_COOKIE_NAME',
  16. 'path': '/',
  17. 'secure': False,
  18. 'value': '8E446DBDE647A599F88988C7F9401BF4'}
  19. browser.add_cookie(c1)
  20. browser.add_cookie(c2)
  21. browser.add_cookie(c3)

add_cookie里每一个键值对的意义:

  • name:cookie的名称
  • value:cookie对应的值,动态生成的
  • domain:服务器域名
  • expiry:Cookie有效终止日期
  • path:Path属性定义了Web服务器上哪些路径下的页面可获取服务器设置的Cookie
  • httpOnly:防脚本攻击
  • secure:在Cookie中标记该变量,表明只有当浏览器和Web Server之间的通信协议为加密认证协议时

接下来可以直接在获取需要登录的网页

  1. browser = webdriver.Chrome()
  2. url = "http://exam.chinapmp.cn/" # 这个是根链接
  3. url1= "http://user.chinapmp.cn/examsign;sign.shtml" #这个是需要登录后才能访问的其他链接
  4. browser.get(url1)

token登录

有的网页不用cookies,有自己的token,没关系,继续试试token

  1. #获取token
  2. token=browser.execute_script('window.localStorage.getItem("token")')
  3. print(token)
  4. #添加token
  5. js='window.localStorage.setItem("token","token值")'
  6. browser.execute_script(js)
  7. browser.refresh()#刷新

全部脚本

  1. import logging
  2. import os
  3. import platform
  4. import time
  5. import selenium.webdriver.support.ui as ui
  6. from selenium import webdriver
  7. from selenium.common.exceptions import NoSuchElementException, TimeoutException
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.common.keys import Keys
  10. from selenium.webdriver.support import expected_conditions as EC
  11. from selenium.webdriver.support.wait import WebDriverWait as WD
  12. from selenium.webdriver.chrome.options import Options
  13. # from time import sleep, time
  14. # 首页界面
  15. login_user = "Login_uName" # id
  16. login_password = "Login_uPass" # id
  17. login_button = "uLogin" # id
  18. sign_entrance = "/html/body/div[3]/div[1]/div[2]/div[1]/div/ul/li[1]/span/a" # xpath
  19. 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
  20. next_button = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody/tr/td[2]/div/a"
  21. yes_button ="clause_yes"
  22. info_confirm ="/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[21]/td/input"
  23. 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"
  24. 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"
  25. last_name ="Xing"
  26. middle_name ="Zhong"
  27. first_name = "Ming"
  28. training_agency = "/html/body/div[2]/div[1]/div[1]/div[2]/div[1]/form/table/tbody/tr[6]/td[1]/select/option[2]"
  29. username_web = "PMIUname"
  30. password_web = "PMIUpass"
  31. dateF = "PMItimeB"
  32. dateF1="/html/body/div[4]/table/tbody/tr[2]/td[1]/a"
  33. dateT = "PMItimeE"
  34. dateT1 ="/html/body/div[4]/table/tbody/tr[5]/td[1]/a"
  35. test_site = "//*[@id='Kaodian']/option[7]" # 大连东软
  36. finish = "//*[@id='vform']/table/tbody/tr[14]/td/input"
  37. logging.basicConfig(
  38. level=logging.WARNING,
  39. format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
  40. )
  41. # 无头模式打开浏览器
  42. __options = Options()
  43. __options.add_argument('--headless')
  44. __options.add_argument('--window-size=1440x900')
  45. __options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
  46. # 无头模式打开浏览器
  47. #browser = webdriver.Chrome(chrome_options=__options)
  48. # 不使用无头模式打开浏览器
  49. browser = webdriver.Chrome()
  50. url = "http://exam.chinapmp.cn/"
  51. url1= "http://user.chinapmp.cn/examsign;sign.shtml"
  52. browser.get(url)
  53. wait2s = ui.WebDriverWait(browser, 2)
  54. wait10s = ui.WebDriverWait(browser, 10)
  55. wait30s = ui.WebDriverWait(browser, 30)
  56. try:
  57. # 登录
  58. browser.get(url)
  59. wait30s.until(EC.title_is('首页-项目管理职业资格认证'))
  60. #BasePage = Page.BasePage
  61. element = browser.find_element_by_xpath(sign_entrance)
  62. element.click()
  63. # BasePage.click("xpath",sign_entrance)
  64. #wait30s.until(EC.title_is('会员登录-首页-项目管理职业资格认证'))
  65. time.sleep(1)
  66. username = browser.find_element_by_id(login_user)
  67. username.send_keys("chai1005") # 请再这儿输入用户名!
  68. password = browser.find_element_by_id(login_password)
  69. time.sleep(1)
  70. password.send_keys("chai1005") # 请再这儿输入密码!
  71. browser.find_element_by_id(login_button).click()
  72. # password.send_keys(Keys.ENTER)
  73. time.sleep(1)
  74. cookies = browser.get_cookies()
  75. print(cookies)
  76. c1= cookies[0]
  77. print("c1: ",c1)
  78. c2 = cookies[1]
  79. print("c2: ", c2)
  80. c3 = cookies[2]
  81. print("c3: ", c3)
  82. # 第一次的时候需要获取cookie,第一次以后可以手动保存起来,一段时间内是有效的,保存为下面格式即可
  83. # c1 = {'domain': '.chinapmp.cn','httpOnly': True,'name': 'user','path': '/','secure': False,'value': 'Te6Im1OPs5FPP88z0Ixj2f9i_yb_Yv8W'}
  84. # c2= {'domain': '.chinapmp.cn','httpOnly': True,'name': 'SYSTEM_TEMP_COOKIE_NAME','path': '/', 'secure': False,'value': 'B3D8E21B1D65DA723114967F114E359D'}
  85. # c3= {'domain': '.chinapmp.cn','expiry': 1634708549, #1634698949'httpOnly': True,'name': 'SYSTEM_ONLY_COOKIE_NAME','path': '/','secure': False,'value': 'B5B26CE39ED2C81F82FDF17D1FC2C4E5'}
  86. browser.add_cookie(c1)
  87. browser.add_cookie(c2)
  88. browser.add_cookie(c3)
  89. browser.get(url1)
  90. # 点击报名
  91. # browser.find_element_by_xpath(i_sign_entrance).click()
  92. # time.sleep(1)
  93. # 点击下一步
  94. # browser.find_element_by_xpath(next_button).click()
  95. # time.sleep(61)
  96. # 点击同意
  97. # browser.find_element_by_id(yes_button).click()
  98. # time.sleep(1)
  99. # 点击个人信息确认无误
  100. # browser.find_element_by_xpath(info_confirm).click()
  101. # time.sleep(1)
  102. # 输入报考信息
  103. browser.find_element_by_xpath(exam_type_1).click()
  104. browser.find_element_by_id(last_name).send_keys("XXXX")
  105. browser.find_element_by_id(middle_name).send_keys("XXXX")
  106. browser.find_element_by_id(first_name).send_keys("XXXX")
  107. browser.find_element_by_xpath(training_agency).click()
  108. browser.find_element_by_id(username_web).send_keys("XXXX")
  109. browser.find_element_by_id(password_web).send_keys("XXXX")
  110. #browser.find_element_by_id(dateT).send_keys(Keys.ENTER)
  111. browser.find_element_by_id(dateF).click()
  112. browser.find_element_by_xpath(dateF1).click()
  113. #browser.find_element_by_id(dateF).send_keys(Keys.ENTER)
  114. browser.find_element_by_id(dateT).click()
  115. browser.find_element_by_xpath(dateT1).click()
  116. browser.find_element_by_xpath(test_site).click()
  117. browser.save_screenshot("./filename.png")
  118. # 点击报名
  119. #browser.find_element_by_xpath(finish).click()
  120. time.sleep(10)
  121. finally:
  122. browser.close()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/384265
推荐阅读
相关标签
  

闽ICP备14008679号