Chrome Issues with Python Selenium on Windows 10 - python

Apologies if this has been answered elsewhere (I'm sure it will have been) but I've spent the past few days searching high and low, trying all the solutions I've come across.
I've been trying to automate some form completion on Chrome using Selenium and Python.
My solution works fine on a personal device but on my company machine I've been hitting some issues.
Running my test code, originally produced the "DevToolsActivePort file doesn't exist" error but I managed to get around that.
The script now opens a Chrome window but then produces an error:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: crashed. (chrome not reachable) (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.)
This is whilst the Chrome window does open and remains open.
Unfortunately with this being a work machine I am unable to reinstall windows or Python at this time.
I've checked the versions and everything appears to line up.
Python: 3.7.0
Selenium: 3.141.0
Chrome: 91.0.4472.114
ChromeDriver: 91.0.4472.101 (also tried other versions)
Code Used:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
options.add_argument("--disable-dev-shm-using")
options.add_argument("--disable-extensions")
options.add_experimental_option("useAutomationExtension", True) #also tried False
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("user-data-dir=C:\\Users\\UP1170\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_experimental_option("detach",True)
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\\Users\\UP1170\\Desktop\\chromedriver.exe")
driver.get("https://www.google.com")
This is not the full code I've developed for the automation but a test script to just try and get Chrome to open on a company windows 10 laptop.
Anyone have any ideas?
I've got a feeling it's going to be linked to a company restriction placed on my device as a similar issue occurs when I've attempted this with both Firefox and Edge.

Related

Python selenium headless start with extensions and xfvb

I'm trying to run a headless Chrome process with an extension1. I'm using WSL.
For testing purposes I can use one of two browsers:
Ubuntu: Google Chrome 86.0.4240.183
Windows: Version 86.0.4240.183 (Official Build) (64-bit)
For production purposes the Ubuntu's Chrome is the only viable option2.
The rest of the stack:
Python 3.6.9
selenium: 3.141.0
In order to overcome Chrome not being able to start headlessly with extensions I use PyVirtualDisplay.
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600), backend='xvfb')
display.start()
options = ChromeOptions()
options.add_extension('/home/plonca/selenium_draft/extensions.crx')
options.add_argument('--no-sandbox')
windows_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver.exe'
ubuntu_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver'
chrome_driver = webdriver.Chrome(executable_path=ubuntu_chrome_driver
, options=options
)
The problem occurs in the last line: if the windows_chrome_driver is set as executable_path everything is fine. If I set ubuntu_chrome_driver instead, I get an error:
WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://bmlddehkgnjdalnfaecgflmmeknlbohi/_generated_background_page.html
from tab crashed
I wonder what the cause of the error might be. How to make chrome_driver work in the headless environment?
1 The extension is needed so that I can authenticate via a pop-up window. The extension has been created according to instructions found here. Other options such as sending username and password in the URL or automating the click with AutoIt doesn't work.
2 Please note that the Chrome seems to be the only option since using Firefox is a matter of an unresolved issue.

Selenium driven ChromeDriver can't find Chrome binary

I just uninstalled Chrome because it was acting strange (fixed now) and after this Selenium in Python is not able to identify the Chrome driver binary, which is extremely strange because it should be completely unaffected and it is in a different location and a different version from the chrome I use on my desktop, code is as follows and has worked for years now.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--load-extension='+exension_path)
driver = webdriver.Chrome(executable_path=chrome_driver_folder,options=chrome_options)
Anyone has any idea what on earth is going on? I get the follow error:
WebDriverException: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.18362 x86_64)
This error message...
WebDriverException: Message: unknown error: cannot find Chrome binary (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.18362 x86_64)
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.40
Release Notes of chromedriver=2.40 clearly mentions the following :
Supports Chrome v66-68
As you have uninstalled Chrome and reinstalled presumably you are using the latest chrome=85.0
Release Notes of ChromeDriver v85.0 clearly mentions the following :
Supports Chrome version 85
So there is a clear mismatch between ChromeDriver v2.40 and the Chrome Browser v85.0
Solution
Ensure that:
Selenium is upgraded to current released Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v85.0 level.
Chrome is updated to current Chrome Version 85.0 level. (as per ChromeDriver v85.0 release notes)
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Execute your #Test as non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
In order to have a clean code and to stop track the chrome path/version, I recommend to you to use webdriver_manager
Install it
pip install webdriver_manager
and use it like this
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
chrome_options.add_argument('--load-extension='+exension_path)
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
but if don't want to use it here is the code for local browser
chrome_options = Options()
chrome_options.add_argument('--load-extension='+exension_path)
chrome_options.binary_location = 'YOUR_PATH'
driver = webdriver.Chrome(executable_path=os.path.abspath(“chromedriver"), chrome_options=chrome_options)
but I totally recommend using the first version.

Chrome not running without headless mode (Ubuntu 18.04)

So I'm working on a script that scrapes some data from a dynamic webpage and commits it to my database tables. For this, I've used Selenium in Python. It all worked perfectly fine until I restarted my system. Now chrome only works in headless mode and when I comment out that option so that I get to see an actual window of the chrome browser, I get this error
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: exited abnormally. (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.)
Solutions tried:
cross-checked the path of my chrome-binaries and it is valid
changed the order of adding options
uninstalled chrome and reinstalled it again
deleted the chromedriver and downloaded it again
restarted my system twice
googled the error and tried the solutions
EDIT: I have already tried adding the --no-sandbox option before commenting it out here as shown in the snapshot
One thing I would want to mention is that my root space is quite less and only 340 mb of free space is left. Does that affect?
To see an actual window of the chrome browser removing the argument -headless is perfect.
However, I would suggest to remove all the unwanted options and execute your test with the minimal lines of code as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('start-maximized')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com/")
In case you see the error as:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. (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.)
You may need to add the argument --no-sandbox.
So the solution is to add back the argument:
chrome_options.addArguments("--no-sandbox");
chrome_options.addArguments("--disable-dev-shm-usage");
You can find a detailed discussion in WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser
Additional considerations
Ensure that:
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v83.0 level.
Chrome is updated to current Chrome Version 83.0 level. (as per ChromeDriver v83.0 release notes)
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test as non-root user.
Reference
You can find a detailed discussion in:
Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python

So I have made a program on one computer using selenium and that worked, Now using it in another computer I get this error:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
Now this same issue was mention in:
Selenium python: Can not connect to the Service %s" % self.path
Selenium python: Can not connect to the Service %s" % self.path
Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver
however the solutions mentioned didnt work.
I am using chrome version 79 and have installed chromedriver 79, I tested writing chromedriver in command line and that works which means path is configured right, I have made sure 127.0.0.1 localhost is also in etc/hosts
Below is my code which works on my computer (so i doubt its an issue with the code):
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome(chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
In the last question I also tried this modification:
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome("C:\\chromedriver.exe",chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
which would instead give me this almost identical error message:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\chromedriver.exe
I am unsure where the issue could lie.
I am using windows by the way.
EDIT: Things I tried and didn't work:
1- running everything as both admin and normal
2- re-installing chrome
3- using the beta-chroma 80 and webdriver 80
4- using normal chrome 79 and webdriver 79
5- Having both the script and the driver in the same directory (while using a
correct path)
6- Having an external path and have it setup as needed
7- Using it in the PATH folder.
8- Adding "127.0.0.1 localhost" to etc/hosts
9- Running service test
I have ensured in every test that everything was in it's correct placement, I have ran a reboot before every new test, and they always give me the same error, which also happens to occur if I had an incorrect path as well, but once I ran the code for a service and it gave me a different error as I had my webdriver in C:/ which required admin privilages, however re-running the test again with the correct privilages gave back the same error
Update the issue isn't exclusive to the chrome driver. Even following setup instructions for either the Firefox or edge drivers end up on the same issues. It makes me suspect that the connection is facing some issue. I have tried running the test codes provided by Mozilla for the setup and it didn't work.
Unsure if that does help much or at all.
This error message...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
You need to take care of a couple of things:
Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:
chromedriver_linux64.zip: For Linux OS
chromedriver_mac64.zip: For Mac OSX
chromedriver_win32.zip: For Windows OS
Ensure that /etc/hosts file contains the following entry:
127.0.0.1 localhost
Ensure that ChromeDriver binary have executable permission for the non-root user.
Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=chrome_options) as driver:
So your effective code block will be:
options = Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox') # Bypass OS security model
options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument("--disable-dev-shm-usage") # overcome limited resource problems
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
Mandatory Considerations
Finally, to avoid incompatibility between the version of the binaries you are using ensure that:
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test as non-root user.
References
You can find a couple of reference discussions in:
Python Selenium “Can not connect to the Service %s” % self.path in linux server
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

Using an adblocker extension with the headless chrome driver using python selenium

I am trying to use an adblocker and running my chrome driver headlessly, doing both separately gives me no error but adding both options gives me the following error:
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html
From what I could understand, the adblock extension is looking for the background page and isn't finding it as it is in headless mode. (it works fine without the headless mode)
Testable code (gives the error):
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# adblocker crx file, downloaded from: https://chrome-extension-downloader.com/
chrome_options.add_extension("Adblocker-for-Chrome-NoAds_v3.2.0.1.crx")
chrome_options.add_argument("headless")
driver = webdriver.Chrome(options=chrome_options)
Things I have tried:
·Different adblocker. (can provide a list)
·Making the code wait at various place. (as the error said it failed to wait for
something)
·Going headless but without an adblocker. (was slower than with an adblocker and the window's GUI showing)
·Asking google multiple times the question. (didn't work...)
If anyone knows a solution to run the chrome browser headless using python selenium while having an adblock extension, I would like to know it too, thanks.
Running selenium in headless mode with extensions is currently not possible and as it seems, Google isn't planning on supporting that any time soon.
You can find more information here

Categories

Resources