1

I've got a Pi4 and using the latest Pi OS I'm trying to take a screenshot of a Chromium webpage (weather map) with Selenium while displaying weather forescast program with Pygame and it works perfectly The only issue is that it "sometimes" causes Pygame to toggle out of fullscreen and not go back to fullscreen after saving the image This only happens sometimes and not all the time

I think the problem could be solved by using Chromium headless but I cannot get this to work Anyone able to help?

    from selenium import webdriver
    from PIL import Image
    import pygame
    driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
    url = "https://www.windy.com/screenshot?53.199,-2.000,7,i:pressure"
    driver.get(url)
    driver.fullscreen_window()
    driver.implicitly_wait(10)
    driver.save_screenshot('weather.png')
    basewidth = 240
    img = Image.open('weather.png')
    driver.quit()
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), Image.ANTIALIAS)
    img.save('/home/pi/images/weather_icons/kia.png')

If I use driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver') --headless it says

  File "map.py", line 4, in <module>
    driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver') --headless
  NameError: name 'headless' is not defined

I have Firefox-esr installed as well but could not get this working and as chromium browser is working I'd like to stick with this "if possible"

kevinnls
  • 728
  • 4
  • 19
Lewis
  • 55
  • 6
  • any error message in particular that you could share? also i may be wrong, but i don't think you can do a screenshot of headless – kevinnls Mar 29 '21 at 12:54
  • @kevinnls screenshot in headless mode does work. It might be required to have all of the necessary libraries installed on the system so that you would be able to render the image if not running in headless mode. – Z4-tier Mar 29 '21 at 12:58
  • i stand corrected. thank you @Z4-tier. OP, does [this](https://stackoverflow.com/a/46929945/13982210) help? – kevinnls Mar 29 '21 at 13:03
  • Many thanks This made it work :) `options = Options() options.headless = True driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=options)` – Lewis Mar 29 '21 at 17:37
  • Does this answer your question? [How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?](https://stackoverflow.com/questions/46920243/how-to-configure-chromedriver-to-initiate-chrome-browser-in-headless-mode-throug) – Z4-tier Mar 29 '21 at 17:46

0 Answers0