我正在尝试在此页面上生成的弹出窗口中接受 cookie 同意。我尝试使用waitForSelector
,但我使用的选择器似乎对无头浏览器不可见。我想实际切换到“是”,然后提交表格。我想它已经显示了,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.')