My desired result is a 1080p screenshot of a dashboard. Since the renderer can not do whole dashboards, I’ve opted to try selenium.
For some reason it takes a screenshot of the background, but no content. Does anyone know what is making the content disappear? I’m not great at inspect mode, but it looks like maybe it preloads the page and then generates the graphs. I have been trying to get python to wait, but nothing seems to work.
from selenium import webdriver
from pyvirtualdisplay import Display
from PIL import Image
# Function to capture webpage screenshot
def capture_screenshot(url, output_file):
# Set up virtual display with xvfb
display = Display(visible=0, size=(1920, 1080))
display.start()
# Set up Selenium WebDriver with Firefox
#driver = webdriver.Firefox()
# Set up Selenium WebDriver with Chrome
driver = webdriver.Chrome()
# Open the webpage
driver.get(url)
# Set window size to 1920x1080 (1080p)
driver.set_window_size(1920, 1080)
# Capture screenshot
driver.save_screenshot(output_file)
# Close the browser
driver.quit()
# Stop the virtual display
display.stop()
# Example usage
if __name__ == "__main__":
#url = "https://google.com"
url = "http://192.168.1.254:8081/public-dashboards/3e2bfdbebdf946c6a401db9be5e07d5f?orgId=1"
output_file = "webpage_screenshot.png"
capture_screenshot(url, output_file)
print("Screenshot saved as", output_file)
Thanks!
Unfortunately too many things have changed since that was posted so the EC statements point to old pointers. The name and password are easy enough to update, but the xpath doesn’t have documentation on what the element being used was or should be now.
I kept fighting it and found out it just needs to wait for the page to render.
Since public dashboards can’t be used in kiosk mode, I might use vishalendu’s login code in v2.
import time
from selenium import webdriver
from pyvirtualdisplay import Display
from PIL import Image
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Function to capture webpage screenshot
def capture_screenshot(url, output_file):
# Set up virtual display with xvfb
display = Display(visible=0, size=(1920, 1080))
display.start()
# Set up Selenium WebDriver with Firefox
driver = webdriver.Firefox()
# Set up Selenium WebDriver with Chrome
#driver = webdriver.Chrome()
# Open the webpage
driver.get(url)
# Set window size to 1920x1080 (1080p)
driver.set_window_size(1920, 1080)
# Sleep for a moment to ensure the page is fully loaded
time.sleep(2)
# Capture screenshot
driver.save_screenshot(output_file)
# Close the browser
driver.quit()
# Stop the virtual display
display.stop()
if __name__ == "__main__":
#url = "https://google.com"
# Public Dashboard
url = "http://192.168.1.254:8081/public-dashboards/3e2bfdbebdf946c6a401db9be5e07d5f?orgId=1"
output_file = "/srv/solar_web/solar.png"
capture_screenshot(url, output_file)
print("Screenshot saved as", output_file)
3 Likes