Connect to Headless with Python Function Stack

In this section you will find the required scripts to connect to headless using the Python function stack with a browser automation library of your preference:

Puppeteer

Using Puppeteer Without SmartBrowz

    
copy
import asyncio from pyppeteer import launch async def main(): browser = await launch() page = await browser.newPage() await page.goto('http://example.com') await page.screenshot({'path': 'example.png'}) await browser.close() asyncio.get_event_loop().run_until_complete(main())

Using Puppeteer With SmartBrowz

    
copy
import asyncio import pyppeteer async def main(): # Replace puppeteer.launch with pyppeteer.launcher.connect browser = await pyppeteer.launcher.connect( browserWSEndpoint='YOUR_CDP_ENDPOINT' ) # The rest of your code remains the same page = await browser.newPage() await page.goto("https://example.com") await page.screenshot(path="screenshot.png") await browser.close() asyncio.get_event_loop().run_until_complete(main())
Note: Copy the CDP Endpoint from the console and paste it in line "6" when you implement this code.

Playwright

Using Playwright Without SmartBrowz

    
copy
from playwright.sync_api import sync_playwright def run(playwright): chrome = playwright.chrome browser = chrome.launch() page = browser.new_page() page.goto("https://example.com") browser.close() with sync_playwright() as playwright: run(playwright)

Using Playwright With SmartBrowz

    
copy
import asyncio from playwright.async_api import async_playwright async def run(playwright): # Replace playwright.chromium.launch with connect_over_cdp browser = playwright.chromium.connect_over_cdp("YOUR_CDP_ENDPOINT") # The rest of the code remians the same context = await browser.new_context() page = await context.new_page() await page.goto("https://example.com") await page.screenshot(path="screenshot.png") await browser.close() async def main(): async with async_playwright() as playwright: await run(playwright) asyncio.run(main())
Note: Copy the CDP Endpoint from the console and paste it in line "5" when you implement this code.

Selenium

Using Selenium Without SmartBrowz

    
copy
from selenium import webdriver from PIL import Image driver = webdriver.Chrome(executable_path = "path\\to\\chromedriver.exe") url = "https://example.com/" driver.get(url) driver.save_screenshot(‘ss.png’) screenshot = Image.open(‘ss.png’) driver.close()

Using Selenium With SmartBrowz

    
copy
import asyncio from selenium import webdriver from selenium.webdriver.chrome.options import Options async def index(): options = Options() options.add_argument("--disable-extensions") options.add_argument("--no-sandbox") options.add_argument('--headless') options.add_argument('--disable-dev-shm-usage') # Change webdriver.Chrome with webdriver.Remote driver = webdriver.Remote( command_executor='YOUR_WEBDRIVER_ENDPOINT', options=options ) #The rest of your code remains the same driver.get("https://example.com") driver.save_screenshot("screenshot.png") driver.close() async def my_async_function(): await index() # call my_async_function asyncio.run(my_async_function())
Note: Copy the Webdriver Endpoint from the console and paste it in line "13" when you implement this code.

Last Updated 2023-05-08 11:50:17 +0530 +0530