How to start Chrome in a real browser with Selenium - selenium

I want to launch Chrome as real browser, with Selenium.
I want to use Chrome extensions, so I want to launch a real browser instead of headless Chrome.
On Mac, this code will successfully launch the Chrome browser
# Selenium ruby
options = Selenium::WebDriver::Chrome::Options.new
::Selenium::WebDriver.for :chrome, options: options
However, when I run the same code on Ubuntu, I get an error.
# error
unknown error: Chrome failed to start: exited abnormally. (Selenium::WebDriver::Error::UnknownError)
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
When I tried to call the command directly to chrome, I got the following error.
$ google-chrome
No protocol specified
[16445:16445:0220/022600.429383:ERROR:browser_main_loop.cc(1438)] Unable to open X display.
Please Help me.

Related

unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist)

I have been trying to figure out an issue I have been experiencing with Selenium + Chrome driver since mid-November where when it tries to start Chrome it gives the following error:
Exception calling ".ctor" with "2" argument(s): "unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)"
The issue just started happening and no updates were made to Chrome, the Chrome driver, or even the Selenium web driver. I have tried to update to the latest versions of Chrome (currently Version 108.0.5359.99 (Official Build) (32-bit)), Chrome Driver (108.0.5359.71), and Selenium Web Driver (C# 4.7.0) but the issue persists.

Selenium Headless : Do I need to install chrome to run test with chromedriver in headless mode [duplicate]

I've tried to search, but haven't found a definitive answer. On Windows Server 2016 WITHOUT Chrome Browser actually installed. I downloaded the correct "chromedriver.exe" and placed it in "D:\Apps\chromedriver.exe". I have added to my environment PATH the full path as "D:\Apps\chromedriver.exe".
When I attempt to start my Windows Service that utilizes the latest Selenium, I get the following error:
Exception occurred: Failed initializing web driver: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)
Question: Do I have to actually install the full-blown browser in addition to the chromedriver, or is this simply just not finding the chromedriver.exe in my Python code (included below for full disclosure):
def __init__(self, username, password, environment='cert'):
self.username = username
self.password = password
self.environment = environment
# Instantiate a chrome options object so you can set the size and headless preference
self.chrome_options = Options()
# Toggle Headless or not
if HEADLESS_TOGGLE == 1:
self.chrome_options.add_argument("--headless")
self.chrome_options.add_argument("--disable-gpu") # Disables "Lost UI Shared Context GPU Error on Windows"
self.chrome_options.add_argument('--disable-extensions') # Disables Extensions
self.chrome_options.add_argument("--disable-software-rasterizer") # Disables "Lost UI Shared Context GPU Error on Windows"
self.chrome_options.add_argument("--window-size=1024x768")
self.chrome_options.add_argument("--log-level=3") # Errors Only
self.chrome_options.add_argument("--incognito") # Keeps history and logs clear
self.chrome_options.add_argument("--no-sandbox")
self.chrome_options.add_argument("--mute_audio") # No loud surprises!
self.chrome_options.add_argument("--no-gpu") # Disables gpu-based errors (headless)
self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
This error message...
Exception occurred: Failed initializing web driver: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)
...implies that the ChromeDriver was unable to find the Chrome binary while trying to initiate a new new Browsing Context i.e. Chrome Browser session.
As per the documentation with in the wiki page of ChromeDriver:
ChromeDriver is a standalone server which earlier implemented the WebDriver's wire protocol but slowly and gradually shifting it's implementation as per WebDriver standard.
The ChromeDriver consists of three separate pieces.
There is the browser itself i.e. chrome
The language bindings provided by the Selenium project i.e. the driver
An executable downloaded from the Chromium project which acts as a bridge between chrome and the driver which is called chromedriver and we refer to it as the server.
In generic scenarios the server expects you to have Chrome installed in the default location for each system:
Linux: /usr/bin/google-chrome 1
Mac: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP: %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista and newer: C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
Note: 1: For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary.
You can find a detailed discussion on overriding the default Chrome binary location in WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome
Solution
So ideally to execute your tests using ChromeDriver / Chrome combo you need to:
Install the full-blown google-chrome browser.
Download the compatible version of chromedriver.exe
Quick installation of ChromeDriver:
Mac users with Homebrew: brew tap homebrew/cask && brew cask install chromedriver
Debian based Linux distros: sudo apt-get install chromium-chromedriver
Windows users with Chocolatey installed: choco install chromedriver
You can find a couple of relevant discussions in:
How to work with a specific version of ChromeDriver while Chrome Browser gets updated automatically through Python selenium
Selenium for ChromeDriver and Chrome Browser and the log message “Only local connections are allowed”
WebDriverException: Message: Service /usr/lib/chromium-browser/chromedriver unexpectedly exited on Raspberry-Pi with ChromeDriver and Selenium
Reference
You can find a detailed discussion in:
WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome
Users provided relevant link to confirm that, "YES" a full Chrome installation is needed in addition to the actual chromedriver.
Link: https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
In my case, I am using PowerShell. I figured out that you need to put the correct chromedriver.exe version, according to the installed Chrome browser, in the path under Selenium Module installation folder, and replace the existing file.
Check this answer for details:
I cannot start chrome instant using PowerShell and Selenium module

Selenium `WebDriverException: Message: unknown error: unable to discover open window in chrome`

Hi.
I am using selenium to control chromium in order to do some low-effort scraping. I am also on linux, inside the crostini container for chromeOS, which complicates things a bit. I get this code running on windows just fine, but inside the linux container, I can't get it to work. It always opens a window (chromium presumably), which then quickly closes and the program fails with the message WebDriverException: Message: unknown error: unable to discover open window in chrome.
relevant code (w/o imports)
options = webdriver.ChromeOptions()
options.add_argument('--mute-audio')
options.add_argument('--log-level=3')
driver = webdriver.Chrome(options=options)
installed packages
chromium
chromium --version: Chromium 102.0.5005.61 built on Debian 11.3, running on Debian 11.3
chromium-driver
chromium-driver --version: ChromeDriver 102.0.5005.61 (0e59bcc00cc4985ce39ad31c150065f159d95ad3-refs/branch-heads/5005#{#819})
Does anybody know how to fix this? I can't seem to find any solution to that by googling...

Pythonanywhere | Selenium Chrome

I do it with the same code. Also add no --no-sandbox.
But not working.
I get this one:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(chrome not reachable)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 5.4.0-1038-aws x86_64)
In general, when you are getting a crash starting Chrome on PythonAnywhere, it's because you have been leaving processes running by not properly closing the browser in your code. Search for "selenium" on the PythonAnywhere help pages for example code that properly closes Chrome and also shows parameters that you should use.

Jenkins can't launch real browser on Ubuntu 16.04

I've selenium script which launches chrome browser, goes to a website and does basic checks.
When I tried running same script locally through jenkins I got error
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.13.0-17-generic x86_64)
This is resolved by Unknown error: Chrome failed to start: exited abnormally
I'm confused why Jenkins can't launch real chrome browser on Ubuntu ?
Because Jenkins server normally does not have a graphical subsystem installed in the operating system. When a real browser (or any app) tries to create a window that fails.
You will need to run your tests on Jenkins using a headless browser - one that does not need a graphical subsystem. Chrome-headless is currently the preferred option, replacing the now abandoned PhantomJS.