gpt4 book ai didi

Python:Pyppeteer 单击弹出窗口

转载 作者:行者123 更新时间:2023-12-02 06:25:24 26 4
gpt4 key购买 nike

我正在尝试接受在此 page 上生成的弹出窗口上的 cookie 同意。 。我尝试使用 waitForSelector 但我使用的选择器似乎对 headless 浏览器不可见。我想实际上切换到"is",然后提交表格。我猜它显示在 window.onload 上,所以也许这需要在 JavaScript 中完成?

import asyncio
import time

from pyppeteer import launch
from pyppeteer.errors import TimeoutError
from urllib.parse import urlparse

URLS = [
'https://www.trustarc.com/'
]

start = time.time()

async def fetch(url, browser):
page = await browser.newPage()
try:
#await page.setRequestInterception(True)
page.on('request', callback)
await page.goto(url, {'waitUntil': 'networkidle0'})
await page.screenshot({'path': f'img/{urlparse(url)[1]}.png', 'fullPage': True})
except TimeoutError as e:
print(f'Timeout for: {url}')
finally:
await page.close()


async def callback(req):
print(f'Request: {req.url}')

async def run():
browser = await launch(headless=True, args=['--no-sandbox'])
tasks = []

for url in URLS:
task = asyncio.ensure_future(fetch(url, browser))
tasks.append(task)

ret = await asyncio.gather(*tasks)
await browser.close()

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run())
loop.run_until_complete(future)

print(f'It took {time.time()-start} seconds.')

最佳答案

如果有人觉得这很有用,这是我基于已接受答案的 Python 实现:

import asyncio
import time

from pyppeteer import launch
from pyppeteer.errors import TimeoutError
from urllib.parse import urlparse

URLS = [
'https://www.trustarc.com/'
]

start = time.time()

async def fetch(url, browser):
page = await browser.newPage()
try:
#await page.setRequestInterception(True)
#page.on('request', callback)
await page.goto(url, {'waitUntil': 'networkidle0'})
if not await page.J('.truste_overlay'):
await page.click('#teconsent > a')
cookies_frame = page.frames[1]
await cookies_frame.waitForSelector( '.active', {'visible': True})
await cookies_frame.evaluate('''() =>
{
const yes_buttons = document.getElementsByClassName( 'off' );
const submit_button = document.getElementsByClassName( 'submit' )[0];

yes_buttons[0].click();
yes_buttons[1].click();

submit_button.click();
}''')
close_button = await cookies_frame.waitForSelector( '#gwt-debug-close_id' )
await close_button.click()
await page.screenshot({'path': f'img/{urlparse(url)[1]}.png', 'fullPage': True})
except TimeoutError as e:
print(f'Timeout for: {url}')
finally:
await page.close()


async def callback(req):
print(f'Request: {req.url}')

async def run():
browser = await launch(headless=True, args=['--no-sandbox'])
tasks = []

for url in URLS:
task = asyncio.ensure_future(fetch(url, browser))
tasks.append(task)

ret = await asyncio.gather(*tasks)
await browser.close()

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run())
loop.run_until_complete(future)

print(f'It took {time.time()-start} seconds.')

关于Python:Pyppeteer 单击弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52221803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com