How to show selenium browser in VNC after creating it over SSH with a virtual display

166 views Asked by At

I created a selenium browser over SSH using like so

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1920, 1920))
display.start()
driver = webdriver.Chrome()

I then logged into the machine with VNC. And my goal is to move the chrome window created there onto my VNC session. However that instance of chrome doesn't even show up in the task bar even though I can tell it is open and even interact with it.

I connected via

webdriver.Remote(command_executor=bla,options=options)

And I did try

driver.switch_to.window(driver.current_window_handle)

But that didn't work. I even tried

driver.switch_to.window(webdriver.Chrome().current_window_handle)

But this gave me a selenium.common.exceptions.NoSuchWindowException

1

There are 1 answers

0
milahu On

by default, manage_global_env is True
so pyvirtualdisplay.Display()
will set os.environ["DISPLAY"] to the virtual display

webdriver will use subprocess.Popen
with the environment os.environ to start chrome

so this should just work...

import os
from pyvirtualdisplay import Display

print("main script is running on display", os.environ.get("DISPLAY"))

display = Display(visible=0, size=(1920, 1920))
display.start()

print("starting chrome on display", os.environ.get("DISPLAY"))
driver = webdriver.Chrome()