analitics

Pages

Saturday, July 18, 2026

Python Qt : testing Browserless.io feature ...

The provided PyQt6 application is a desktop tool that allows a user to input any website address and instantly obtain a full‑page screenshot of that site using the Browserless.io cloud rendering service; the program begins by creating a simple graphical interface composed of a text field for entering a URL, a button that triggers the screenshot process, and a display area where the resulting image is shown, all arranged vertically for clarity and ease of use. When the user presses the button, the application reads the typed URL, constructs a request to the Browserless screenshot API using the user’s authentication token, and sends a JSON payload instructing Browserless to load the page and capture it in full, not just the visible viewport. Browserless processes the request by launching a headless Chromium instance, navigating to the specified website, rendering the page completely—including dynamic content—and returning a PNG image representing the entire scrollable webpage. The application receives this binary PNG data through an HTTP response, converts it into a QPixmap object, and displays it directly inside the PyQt window, effectively turning the GUI into a live screenshot viewer. This design allows developers to visually inspect websites without embedding a browser engine locally, relying instead on Browserless’s remote infrastructure to handle rendering, JavaScript execution, and screenshot generation. The code is intentionally minimal, focusing on clarity and functionality: it uses only standard PyQt widgets, avoids unnecessary complexity, and demonstrates how to integrate cloud‑based browser automation into a Python desktop application. The result is a lightweight tool that can preview websites, capture full‑page screenshots, and serve as a foundation for more advanced features such as zooming, saving images, asynchronous loading, or automated website testing workflows.
This script was created with copilot artificial intelligence, and tested by me.
import sys
import requests
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QLabel
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt

# PUNE TOKEN-UL TAU AICI, CA STRING
BROWSERLESS_TOKEN = "fill_TOKEN_official__website"

class BrowserlessViewer(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Browserless Web Screenshot Viewer")
        self.resize(900, 700)

        layout = QVBoxLayout(self)

        self.url_edit = QLineEdit()
        self.url_edit.setPlaceholderText("Introdu adresa web...")
        layout.addWidget(self.url_edit)

        self.btn = QPushButton("Generează Screenshot")
        self.btn.clicked.connect(self.take_screenshot)
        layout.addWidget(self.btn)

        self.canvas = QLabel()
        self.canvas.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(self.canvas)

    def take_screenshot(self):
        url = self.url_edit.text().strip()
        if not url:
            return

        api_url = f"https://chrome.browserless.io/screenshot?token={BROWSERLESS_TOKEN}"

        payload = {
            "url": url,
            "options": {
                "fullPage": True
            }
        }

        try:
            r = requests.post(api_url, json=payload)
            r.raise_for_status()

            pixmap = QPixmap()
            pixmap.loadFromData(r.content)
            self.canvas.setPixmap(pixmap)

        except Exception as e:
            print("Eroare:", e)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    viewer = BrowserlessViewer()
    viewer.show()
    sys.exit(app.exec())