pyppeteer offline test

# encoding=utf-8
import asyncio
import time
import json

from pyppeteer import launch


def open_url(url):
    async def get_data(url):
        # browser = await launch(headless=False, userDataDir='./userdata', args=['--disable-infobars','--window-size=1920,1080',])
        # browser = await launch(headless=False, args=['--disable-infobars','--window-size=1920,1080',])
        # await browser.pages()
        browser = await launch({
            'handleSIGINT': False,
            'handleSIGTERM': False,
            'handleSIGHUP': False,
            'headless': False,
            'dumpio': True,
            'args': [
                '--no-sandbox',
                '--no-default-browser-check',
                '--disable-extensions',
                '--hide-scrollbars',
                '--disable-bundled-ppapi-flash',
                '--mute-audio',
                '--disable-setuid-sandbox',
                '--disable-gpu',
                "--window-size=1500,900",
                "--disable-infobars"
            ],
        })
        page = await browser.newPage()
        await page.setUserAgent(
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
        await page.setViewport({"width": 1920, "height": 1080})

        login_url = url
        await page.goto(login_url)
        await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                         '{ webdriver:{ get: () => undefined } }) }')

        await page.waitForSelector('#fm-login-id')
        await page.type('#fm-login-id', '', {'delay': 120})
        await page.waitForSelector('#fm-login-password')
        time.sleep(0.5)
        await page.type('#fm-login-password', '', {'delay': 120})
        await page.keyboard.press('Enter')
        time.sleep(1.5)

        cookies = await page.cookies()
        await page.close()
        await browser.close()
        time.sleep(3)

        return cookies

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    return loop.run_until_complete(get_data(url))


if __name__ == '__main__':
    # url = ""
    cookies = open_url(url)
    cookie = []
    for c in cookies:
        cookie.append(c['name']+'='+c['value'])
    format_cookie = ';'.join(cookie)
    print(format_cookie)

你可能感兴趣的:(pyppeteer offline test)