web⾃动化中如何使⽤cookie登录
前⾔
  做web⾃动化的时候,登录是关键的⼀步。但每次频繁的输⼊⽤户名、密码导致⼼态有些爆炸,所以使⽤cookie登录势在必⾏。下⾯是两种场景的cookie登录。
cookie登录1
  这种是界⾯地址跳转的情况,是⼤多数登录场景。
  test_login_sg.py
fig import COOKIE_PATH
from po.demo import Sg
import time
from common.utils import get_driver
def account_login():
"""
账号密码登录
"""
driver = get_driver()
page = Sg(driver)
<(page.url)
page.phone.send_keys('***')
page.password.send_keys('***')
page.sure.click()
page.shop_name.click()
cookies = _cookies()
with open(COOKIE_PATH, 'w+') as f:
f.write(str(cookies))
return driver
def cookie_login():
"""
cookie登录
"""
driver = get_driver()
selenium怎么使用page = Sg(driver)
<(page.url)
with open(COOKIE_PATH, "r") as f:
cookie_li = ad())
page.add_cookies(cookie_li)
<(page.shop_url)
time.sleep(30)
if__name__ == '__main__':
# account_login()
cookie_login()
  sg_page.py
from poium import Page, Element
class Sg(Page):
url = "/#/mobileLogin"
shop_url = "/m/#/boss/homeCenter/home?shopId=451"
phone = Element(xpath='//input[@placeholder="请输⼊⼿机号"]')
password = Element(xpath="//input[@placeholder='请输⼊密码']")
sure = Element(xpath="//div[text()='确认']")
shop_name = Element(xpath="//span[text()='砬之眼']")
cookie登录2
  这种情况是url不跳转的情况,常见于登录某⽹站。
  test_login_veer.py
def login_veer():
"""
账号密码登录veer
"""
page = VeerLoginPage(driver)
<(page.url)
page.login_entrance.click()
page.phone.send_keys('***')
page.password.send_keys('***')
page.login.click()
page.picture.click()
cookies = _cookies()
with open(COOKIE_PATH, 'w+') as f:
f.write(str(cookies))
return driver
def login_veer_in_cookie():
"""
使⽤cookie登录
"""
page = VeerLoginPage(driver)
<(page.url)
with open(COOKIE_PATH, "r") as f:
cookie_li = ad())
page.add_cookies(cookie_li)
<(page.url)
time.sleep(40)
return driver
  veer_page.py
class VeerLoginPage(BasePage):
login_entrance = Element(partial_link_text="登录")
phone = Element(xpath="//input[@placeholder='请输⼊您的⼿机号/邮箱']")
password = Element(xpath="//input[@placeholder='请输⼊密码']")
login = Element(xpath="//button[text()='登录']")
person = Element(xpath="//div[@class='wide_header right_off_canvas_toggle_menu']")
log_out = Element(xpath="//a[text()='退出登录']")
success_flag = Element(xpath="//figure[@class='account']")  # 登录完⾸页成功标识
picture = Element(xpath="//nav/a[text()='照⽚']")
后记
新功能调整后使⽤cookie登录不上?
  问题在于cookie,为什么cookie有问题呢?最近新功能有上线,前后端代码有调整,导致获取不到cookie依赖的关键信息,但是开发对于这些并不⾃知,怎么解决?  去⼦级页⾯获取cookie。
没有新功能,以前好好的,现在登录不上?
  服务器如阿⾥有反爬策略,服务区端有很多针对selenium的策略,只要⼈家配置了,⾮常规操作会被受限,怎么解决?
  运维加⽩名单。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。