使用Playwright模拟登陆

import asyncio
from playwright.async_api import async_playwright
​
## jupyter中需要导入下面的包
import nest_asyncio
nest_asyncio.apply()
​

async def handle_response(response, tokens_container):
    if response.url == 'https://cognito-idp.us-west-2.amazonaws.com/':  
        json_data = await response.json()
        if 'AuthenticationResult' in json_data:
            tokens_container['tokens'] = {
                'access_token': json_data['AuthenticationResult']['AccessToken'],
                'refresh_token': json_data['AuthenticationResult']['RefreshToken']
            }

async def handle_request(request, x_api_key_container):
    if 'x-api-key' in request.headers:
        x_api_key_container['x_api_key'] = request.headers['x-api-key']

async def login_and_get_tokens(email, password):
    tokens_container = {}
    x_api_key_container = {}

    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()

        # 注册响应和请求事件监听器
        page.on("response", lambda response: handle_response(response, tokens_container))
        page.on("request", lambda request: handle_request(request, x_api_key_container))
        
        # 导航到指定URL并登录
        await page.goto("https://apply.commonapp.org/login")
        await page.fill('input[name="email"]', email)
        await page.fill('input[name="password"]', password)
        await page.click('button[type="submit"]')
        await page.wait_for_load_state("networkidle")

        await browser.close()
        return tokens_container.get('tokens'), x_api_key_container.get('x_api_key')

# 示例调用
tokens, x_api_key = asyncio.run(login_and_get_tokens("your_email@example.com", "your_password"))

# 检查是否未成功获取 tokens 或 x-api-key
if not any([tokens, x_api_key]):
    print("Failed to retrieve tokens or x-api-key.")
else:
    print("Tokens:", tokens)
    print("x-api-key:", x_api_key)

handle_response 主要用来抓包登陆过程中的请求和响应,可根据需求自行设计

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Playwright可以模拟手机设备进行测试。通过使用`playwright.devices`来注册设备参数,可以模拟特定设备的浏览器行为,包括用户代理、屏幕尺寸、视口以及是否启用触摸。然后可以在浏览器上下文中使用这些参数来创建新的页面,实现对手机设备的模拟。 在引用的代码示例中,我们可以看到两种不同的使用方法。第一个示例使用同步API,通过`playwright.sync_api`引入相关模块。在`run`函数中,使用`playwright.devices['iPhone 12']`选择了iPhone 12作为模拟设备,并在浏览器上下文中使用该设备参数创建了新的页面。 第二个示例使用异步API,通过`playwright.async_api`引入相关模块,并使用`asyncio`库进行异步处理。同样地,在`run`函数中,选择了iPhone 12作为模拟设备,并在浏览器上下文中使用该设备参数创建了新的页面。 总之,通过Playwright的`playwright.devices`可以方便地配置和模拟手机设备,实现对移动端的测试和开发。123 #### 引用[.reference_title] - *1* *3* [python+playwright 学习-17.模拟手机浏览器测试](https://blog.csdn.net/qq_27371025/article/details/129581989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* [Playwright 模拟浏览器、模拟手机、忽略图片加载、等待、监听、操作事件](https://blog.csdn.net/lilongsy/article/details/126587663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值