pyppeteer有关frame的使用

本文介绍了一种利用Python的PyChrome库实现浏览器自动化的方法,包括去除自动化标识、设置浏览器参数、模拟用户行为进行页面浏览及表单提交等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    async def pyChrome(self):
        if '--enable-automation' in launcher.DEFAULT_ARGS:
            launcher.DEFAULT_ARGS.remove("--enable-automation")
        browser = await launch(
            {'handleSIGINT': False, 'handleSIGTERM': False, 'handleSIGHUP': False},
            headless=False, dumpio=True, userDataDir=r'./text',
            args=['--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled'])
        page = await browser.newPage()
        await page.setViewport({ # 最大化窗口
            "width": 1600,
            "height": 800
        })
        try:
            await page.setUserAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Mobile Safari/537.36")
            url = 'https://www.espn.com/watch/schedule/_/type/live/country/us/redirected/true'
            await page.goto(url, {'waitUntil': ['load', 'domcontentloaded', 'networkidle0']})
            # await page.waitForNavigation()
            await page.click('button.Button--unstyled')
            li  = await page.xpath('//ul[@class="Nav__Dropdown__Menu__List flex-expand"]/li')
            if len(li)<=7:
                await li[6].click()
                await page.waitFor(3000)
                pframes = page.frames
                for i in pframes:
                    for j in i.childFrames:
                        con = await j.xpath('//div[@class="content ng-scope"]')
                        if con:
                            user = await con[0].xpath('//input[@type="email"]')
                            pwd = await con[0].xpath('//input[@type="password"]')
                            submit = await con[0].xpath('//button[@type="submit"]')
                            await user[0].type('1111111')
                            await pwd[0].type('1111111')
                            await submit[0].click()

frame嵌套的可以通过childFrames查找嵌套的子frame

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值