I'm using selenium for python with chromedriver. Unfortunately I havn't found a way to handle errors raised by chromedriver in python!
If I use selenium to open any Webseite:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
opts = Options()
prefs = {"profile.managed_default_content_settings.images": 2}
opts.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=opts)
driver.delete_all_cookies()
driver.get("https://www.google.de/")
#Some more actions
driver.close()
And while the script is running if I close the browser window (manually), I get the following error:
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)
If I put the python code above entirely in a try/except bracket, if chrome brakes, the exception is not executed! Instead the entire programm brakes!
Is there a way to handle the error raised by chromedriver in python?
Thanks for your advise!
EDIT:
I guess my question wasn't expressed very well. My script is working fine, I just want to handle the exception if someone closes the browser window manually. For now my entire python tool including my gui crashes...
There is a bug in selenium right now when you use driver.close() as the last step of your script. You should be using driver.quit(). driver.close() is to just close the current window (tab) and leave the browser open. driver.quit() tells selenium to quit the chromedriver service as well
This solution works with me:
sudo apt-get install chromium-chromedriver
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
Related
I want to run selenium in google colab i ran the following code:
!apt update
!apt install chromium-chromedriver
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome(options=options)
wd.get("https://www.whoscored.com")
print(wd.page_source) # results`
the result expected is the page source being printed out instead i get this error in my notebook:
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1
Here is the link of the notebook:
https://colab.research.google.com/drive/13oUHGDc6uy-4vJ1XnRdEcojoFoIToV75
i tried changing the webdriver to firefox but i get a different error memory related stuff:
src/tcmalloc.cc:332] Attempt to free invalid pointer 0x24680020c5d0
I found out the solution from Selenium use chrome on Colab got unexpectedly exited.
Looks like it was a runtime issue. Changing it and following the steps in the link helped and I executed the code there to fix the problem
There are about 100 posts about the same issue but none of them seem to work for me, hence asking again. I'm trying to launch a Firefox browser using Python and Selenium and I get the following error:
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
I tried each and every answer on the web but nothing seems to work.
This is my code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = False
binary = FirefoxBinary('d:\\Desktop\\IEDriver\\geckodriver.exe')
options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_binary=binary, firefox_options=options, executable_path=r'd:\\Desktop\\IEDriver\\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()
If I set caps["marionette"] = True then the error I get is
SessionNotCreatedException: Message: Unable to find a matching set of capabilities
Versions of software I'm running:
Firefox: 62.0 (64 bit)
Selenium: 3.14.0
Gecko: 0.21.0
Python: 3
OS: Windows 8.1 64 bit
Any help would be highly appreciated.
EDIT: I've uninstalled and re-installed Firefox but didn't work. Also tried installing Firefox 61.0.2, still no luck.
This error message...
WebDriverException: Message: The browser appears to have exited before we could connect.
If you specified a log_file in the FirefoxBinary constructor, check it for details.
...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.
You need to take care of a couple of things as follows:
To set the FirefoxBinary you need to use the FirefoxOptions() and instead of passing the absolute path of geckodriver binary, you have to pass the absolute path of the desired firefox binary.
As you are using GeckoDriver v0.21.0 you have to mandatorily use marionette so either keep it unchanged (by default true) or set marionette to true.
Your own code with incorporating the minor changes will be:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.set_headless(headless=True)
options.binary = binary
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True #optional
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()
Console Output:
Headless Firefox Initialized
Here you can find a detailed discussion on Unable to find a matching set of capabilities with selenium 3.4.3, firefox 54.0 and gecko driver 0.17
Make sure (especially on Windows (Win 10)) that your browser and controller (python/C/java/perl/etc) is all either x64 or win32, Microsoft will not thunk between them anymore.
So, if your trying to control a 64 bit browser (what will be downloaded by default from firefox) from a x32 bit python, it will exit before you can connect.. go and install a win32 version of firefox for the magic to happen
After trying almost all of the answers on different forums, a simple self trial resolved the problem and i.e. you need to have python, firefox browser and geckodriver in either 62 bit or 32 bit. Mismatch in this caused the problem in my case.
After ensuring that you are using the same bit version for all the 3 components, just use following lines to run firefox:
ffPath = "C:\\Drivers\\geckodriver.exe"
os.environ["webdriver.firefox.driver"] = ffPath
driver = webdriver.Firefox(executable_path=ffPath)
driver.get(url)
The issue for me was the mis-match version between python and gekodriver . once all the involved party were 64 bit it worked like a charm
This error has troubled me a lot.
Install this:
pip install -U selenium
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.binary = binary
browser = webdriver.Firefox(firefox_options=options, executable_path=r"C:\Drivers\geckodriver.exe")
i use selenium for instagram appicatin.
i wonder there is any way to run selenium without browser opening in windows?
there is some solution in linux like install Xvfb but i don't see any solution for windows!
Do you mean that --headless?
You can do some operations without opening Chrome if you have chrome-driver.
Here is how to set headless:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeoption = Options()
chromeoption.add_argument('--headless')
browser = webdriver.Chrome(options=chromeoption)
# Then it is your work.
browser.get(url="xxx")
This question already has answers here:
Can't open browser with Selenium after Firefox update
(7 answers)
Closed 6 years ago.
I was running some scripts I had developed in Selenium-Python today. They were working just fine. When I closed out a window & went to re-run a test, the Firefox browser that opened up would crash & my script would fail. This literally happened one second to the next. I wasn't sure what had changed to cause this.
I'm running Selenium 2.53 & Firefox 47 on my machine. Occasionally when I try to run my script again, I will get this run error in Pycharm:
File "C:\Python34\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 98, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
Normally I just get an error when I manually close the crashed FireFox browser that opens. Any ideas as to what occurred?
I also had issues with Firefox 47 and Selenium. You could try reverting back to a previous version of Firefox and disabling updates.
Previous versions of Firefox and instructions on how to disable automatic updates can be found here:
https://support.mozilla.org/en-US/kb/install-older-version-of-firefox
According to
Firefox 47 release notes (June 7, 2016):
Unresolved: Selenium WebDriver may cause Firefox to crash on startup, use Marionette WebDriver instead
I would like to slightly correct previous answer. Working example is below:
Preliminary:
Download geckodriver.zip from https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver;
Extract geckodriver.exe to the directory where this script is located.
Run script:
# -*- coding: utf-8 -*-
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
browser = webdriver.Firefox(capabilities=firefox_capabilities, executable_path=gecko+'.exe')
browser.get('http:///www.google.com')
browser.close()
# browser.quit()
I've used Firefox with Webdriver on this machine before, but suddenly when I use any code that utilizes Firefox as the driver, Firefox will open and just sit there blankly. Absolutely nothing loads, my program hangs until I close the window at which point it raises the "browser not open" error.
Tried reinstalling. Chrome/IE Drivers work but I don't really want to use them.
Included a pic of exactly what happens when I use any code, for instance
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
url = "http://google.com"
driver.get(url)
Even this gets me nothing.
upgrade to selenium version 2.29.0
pip install -U selenium
there was a bug fix in the previous version