Conectarse a Headless con el Function Stack de Python
En esta sección encontrará los scripts necesarios para conectarse a headless usando el function stack de Python con la biblioteca de automatización de navegador de su preferencia:
Puppeteer
Usando Puppeteer Sin SmartBrowz
copy
import asyncio
from puppeteer 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())
Usando Puppeteer Con SmartBrowz
copy
import asyncio
import pyppeteer
async def main():
# Reemplazar puppeteer.launch con pyppeteer.launcher.connect
browser = await pyppeteer.launcher.connect(
browserWSEndpoint='YOUR_CDP_ENDPOINT'
)
# El resto de tu código permanece igual
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())
Nota: Copie el CDP Endpoint de la consola y péguelo en la línea "6" cuando implemente este código.
Playwright
Usando Playwright Sin 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)
Usando Playwright Con SmartBrowz
copy
import asyncio
from playwright.async_api import async_playwright
async def run(playwright):
# Reemplazar playwright.chromium.launch con connect_over_cdp
browser = playwright.chromium.connect_over_cdp("YOUR_CDP_ENDPOINT")
# El resto del código permanece igual
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())
Nota: Copie el CDP Endpoint de la consola y péguelo en la línea "5" cuando implemente este código.
Selenium
Usando Selenium Sin 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()
Usando Selenium Con 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')
# Cambiar webdriver.Chrome por webdriver.Remote
driver = webdriver.Remote(
command_executor='YOUR_WEBDRIVER_ENDPOINT',
options=options
)
#El resto de tu código permanece igual
driver.get("https://example.com")
driver.save_screenshot("screenshot.png")
driver.close()
async def my_async_function():
await index()
asyncio.run(my_async_function())
Nota: Copie el Webdriver Endpoint de la consola y péguelo en la línea "13" cuando implemente este código.
Última actualización 2026-03-30 13:40:30 +0530 IST
Yes
No
Send your feedback to us
Skip
Submit