4

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

AlbinoRhino
  • 467
  • 6
  • 23

3 Answers3

1

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

kmpatel100
  • 108
  • 5
0

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.

Frederik Bode
  • 2,632
  • 1
  • 10
  • 17
  • Ive added to the question, to clarify that the solution must work on windows servers – AlbinoRhino Jan 04 '21 at 15:33
  • I should also clarify, im not THAT familiar with running from the command line, and what I make needs to be something my subordinates can open in python and run in my absence – AlbinoRhino Jan 05 '21 at 00:09
0

I know the question has been a little while... I just found out this using PyAutoGUI on remote machines or headless machines at here https://github.com/asweigart/pyautogui/issues/133

Can look at this guys workaround: http://fredtantini.free.fr/blog/index.php?article58/automatiser-des-actions-avec-selenium-pyautogui-et-xvfb

Thank you

  • 1
    Link only answers are discouraged. Links can change over time rendering the answer meaningless. Please summarize the linked information to help future readers. – DaveL17 Aug 05 '21 at 21:22