Can you set a screen size and use pyautogui / selenium chrome driver without a monitor

Viewed 1664

Does anyone know how to force pyautogui or python in general to recognize a set screen size and take in display data, even if no display is connected?

I have a task that uses pyautogui, and selenium chrome driver, which both require a display, or they fail.

It runs on a server, so the start of the program requires my laptop to remote desktop into the server, allowing it to have a display, which allows launching a page with chromedriver, and pyautogui click components / screen search to work.

The issue arises that should my home network ever be down, it cannot kick off the remote desktop, and therefore my automation would fail.

My solution would be to emulate or force the program to behave as if a display existed, so it can just be run server side.

All of my servers are windows, so XVFB does not seem to be an option based on Xvfb on Windows

3 Answers

Well I am using something similar on regular basis, I am using Windows server to run my automated python script which uses selenium webdriver. so, first your answer is you need to use code for screen size in the script and you can run that script with Windows task scheduler so, you don't have to touch your laptop or desktop to run remote desktop. you can use chrome options if you are using chrome as headless browser.

I also, suggest if you are using server which is controlled by some third party who provides regular updates then, you can use chromedriver_autoinstaller package to take updated or supported version chrome driver according to your current version of chrome.

code:

import chromedriver_autoinstaller
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from selenium.webdriver.chrome.options import Options
chromedriver_autoinstaller.install()
#maximize the chrome
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('website address')

For automating task Windows task scheduler is the best option. you can refer this documentation or find something according to your need.

https://towardsdatascience.com/automate-your-python-scripts-with-task-scheduler-661d0a40b279

Note: If you just need to set screen size then your answer start here #maximize the chrome

You should emulate your display driver. Run:

xvfb-run --server-args="-screen 0 1024x768x24" python my_script.py

to launch your script instead of just python my_script.py.

Related