Is there a Windows equivalent to PyVirtualDisplay
Asked Answered
Z

2

16

I have written a web scraper for a mate to save him time at work. It is written in Python, using Selenium and opening a Firefox browser.

I have written this code myself on a Linux machine I use PyVirtualDisplay so Firefox doesn't actually open and disturb my work.

How can I make it run within a virtual display on a Windows PC?

Zircon answered 7/4, 2017 at 5:44 Comment(0)
P
9

The reason you can not run PyVirtualDisplay on Windows is that PyVirtualDisplay uses Xvfb as it's display and Xvfb is a headless display server for the X Window System, Windows does not use the X Window System.

not recommended

So... what you can do if you insist on working with PyVirtualDisplay is to change the Display(visible=True) Or you can set the backend as is shown in the API here.

My recommendation

Don't use PyVirtualDisplay you can use any webdriver such as Chrome driver and just add ChromeOptions with --headless.

Or in your case you use firefox so it would look something like:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')
driver.quit()

For more updated info just have a look here.

Hope this helps you!

Prism answered 8/10, 2018 at 10:46 Comment(0)
E
0

As pointed out by Moshe, there is no direct equivalent of Xvfb on Windows.

But you can simulate it's functionality by using a virtual display.

One option i've found is the Virtual Display Driver available on GitHub here.

After installation, this driver creates a virtual second screen and you can utilize the argument --window-position to position Chrome on this virtual display, effectively keeping it off the main screen.

# Calculate the window position for the virtual display
# based on yout requirements
x = ... # window's X position
y = ... # window's Y position

options = ChromeOptions()
options.add_argument(f"--window-position={x},{y}")
options.add_argument("--start-maximized")

You can also hide it from the taskbar by installing the pygetwindow package:

import pygetwindow

chrome_window_title = "<expected Chrome's window title>"
chrome_window = pygetwindow.getWindowsWithTitle(chrome_window_title)[0]
chrome_window.hide()

Although it is a bit cumbersome, I found this method works nicely even when the --headless argument is detected and the scraper is rejected

Eneidaenema answered 12/7 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.