微软开源的浏览器自动化工具-Playwright
Python七号
共 3227字,需浏览 7分钟
·
2020-11-02 08:11
python -m playwright codegen
pip install playwright
python -m playwright install
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('http://whatsmyuseragent.org/')
page.screenshot(path=f'example-{browser_type.name}.png')
browser.close()
import asyncio
from playwright import async_playwright
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto('http://whatsmyuseragent.org/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
def test_playwright_is_visible_on_google(page):
page.goto("https://www.google.com")
page.type("input[name=q]", "Playwright GitHub")
page.click("input[type=submit]")
page.waitForSelector("text=microsoft/Playwright")
> from playwright import sync_playwright
> playwright = sync_playwright().start()
# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to see the browser UI
> browser = playwright.chromium.launch()
> page = browser.newPage()
"http://whatsmyuseragent.org/") > page.goto(
"example.png") > page.screenshot(path=
> browser.close()
> playwright.stop(
from playwright import sync_playwright
with sync_playwright() as p:
browser = p.firefox.launch()
page = browser.newPage()
page.goto('https://www.example.com/')
dimensions = page.evaluate('''() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
}
}''')
print(dimensions)
browser.close()
from playwright import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.newPage()
def log_and_continue_request(route, request):
print(request.url)
route.continue_()
# Log and continue all network requests
page.route('**', lambda route, request: log_and_continue_request(route, request))
page.goto('http://todomvc.com')
browser.close()
评论