I am using Google Chrome 114 with python 3.8 and selenium v. 4.0.0 onto an Ubuntu Server machine.
This is my code that works perfectly
class Chrome:
def __init__(self):
self.s = ChromeService(DRIVER_PATH, log_path=GECKO_LOG_PATH)
self.chrome_options = Options()
self.chrome_options.add_argument("--headless")
self.chrome_options.add_argument("start-maximized")
self.chrome_options.add_argument("disable-infobars")
self.chrome_options.add_experimental_option(
"excludeSwitches",
[
'enable-automation',
'disable-default-apps',
'disable-translate'
]
)
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--window-size=1920,1080")
self.chrome_options.add_argument("--lang=en")
prefs = {
"translate_whitelists": {
"en":"es",
"jp":"es",
},
"translate":{"enabled":"true"},
}
self.chrome_options.add_experimental_option("prefs", prefs)
Since I cannot use it on ubuntu server without desktop env, I have to start chrome in headless mode but without the auto translating feature.
My questions are:
Thanks.
You can use a virtual display in order to run selenium in headed mode on a headless display, which should resolve your issue.
There are two popular libraries for Python virtual displays:
Here's an example of using sbvirtualdisplay
:
from sbvirtualdisplay import Display
display = Display(visible=0, size=(1440, 1880))
display.start()
# Run browser tests in a headless environment
display.stop()
If you want to make sure the display is stopped, even if a script fails before the end of it, use the context manager format:
with Display(visible=0, size=(1440, 1880)):
# Run browser tests in a headless environment
...