zoukankan      html  css  js  c++  java
  • python selenium 浏览器登录并获取cookie

    一、各模块说明

    selenium.webdriver                                  浏览器驱动
    selenium.webdriver.common.by                        选择器类型
    selenium.webdriver.support.wait.WebDriverWait       等待控制
    selenium.webdriver.support.expected_conditions      条件控制
    selenium.common.exceptions                          异常类型

    二、导入相关模块

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException

    三、加载驱动

    # 按驱动类型加载驱动
    if Path('./chromedriver.exe').exists():
        driver = webdriver.Chrome()
    elif Path('./geckodriver.exe').exists():
        driver = webdriver.Firefox()
    else:
        print('异常:未找到浏览器驱动')
        print('提示:请下载对应版本的浏览器驱动,并放置于目录:' + os.getcwd())
        print('chrome: http://npm.taobao.org/mirrors/chromedriver/')
        print('Firefox: http://npm.taobao.org/mirrors/geckodriver/')
        exit(0)

    四、打开网页

    # 窗口最大化
    driver.maximize_window()
    # 打开网页
    driver.get('https://xxxxx')

    五、元素获取及点击

    driver.find_element(By.CSS_SELECTOR, '.btn-login').click()

    注:这里用的是 css 选择器,其他选择器参考这里:定位元素 | Selenium

    六、输入账号密码并提交登录

    driver.find_element(By.CSS_SELECTOR, '.phonenum_input').send_keys(phone)
    driver.find_element(By.CSS_SELECTOR, '.password_input').send_keys(password)
    driver.find_element(By.CSS_SELECTOR, '.login_submit').click()

    七、等待登录成功后获取cookie

    locator = (By.CSS_SELECTOR, '.user')
    encrypted = '%s****%s' % (phone[:2], phone[-2:])
    WebDriverWait(driver, 3).until(EC.text_to_be_present_in_element(locator, encrypted))
    cookies = driver.get_cookies()
    Path('./cookies.txt').write_text(json.dumps(cookies))
    print('登录成功')
    driver.quit()

    八、使用保存的cookie请求其他接口

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62'}
        
    cookies = Path('./cookies.txt').read_text()
    cookies = {x['name']: x['value'] for x in json.loads(cookies)}
    
    try:
        r = requests.get(url, data=data, headers=headers, cookies=cookies, timeout=5)
        return r.json()
    except requests.exceptions.Timeout:
        print('Error: request timeout.')
        return None
    except json.decoder.JSONDecodeError:
        print('Error: json decode error.')
        return None

    九、selenium 的其他操作

    https://www.selenium.dev/zh-cn/documentation/webdriver/browser_manipulation/


    完。

  • 相关阅读:
    Vue3.0官方文档
    简单实现Vue的双向绑定原理
    小程序使用weapp-qrcode二维码插件,宽高自适应解决方法
    小程序判断ios还是android
    手写实现bind
    手写实现call,apply函数
    React onClick点击事件传参三种写法
    zynq 中断
    zynq_ps端点亮led灯代码
    突然发现自己的很多博客无法显示图片,人都傻了,于是就整理了一早上,全部换成了markdown格式,就好了,希望博客的时间不会对大家造成困扰!!!
  • 原文地址:https://www.cnblogs.com/tujia/p/15129411.html
Copyright © 2011-2022 走看看