Selenium and Firefox issues [duplicate] - python

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")

Related

I can't get Opera to work on selenium python

I am trying to get Opera to work but there is always an error
I followed this thread, and have exactly the same issues Unable to launch Opera using Python Selenium
They didn't solve the problem
Then same OP did this URL opening fails with Opera using python selenium library and was closed because of reposting the same issue (was not the same issue, now it has another error)
this time is this one:
from selenium import webdriver
from selenium.webdriver.opera.options import Options
options = Options()
options.binary_location = OperaPath #path to .exe
driver = webdriver.Opera(options=options, executable_path= OperaPath)
Opera.exe unexpectedly exited. Status code was: 0
operadriver=86.0.4240.80
Opera 72
Windows 10, 64 bits
Python 3.8.1

Python 3.4 Selenium handle chromedriver exception

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")

Selenium webdriver seems not working properly with Firefox 49.0. Am I missing something?

I have gone through previous questions but did not find anyone else running into my issue.
This simple code hangs
from selenium import webdriver
d = webdriver.Firefox();
The browser gets launched, but it just hangs there. After sometime, it crashes and I get the error
selenium.common.exceptions.WebDriverException: Message: Can't load the profile.
Profile Dir: /tmp/tmpn_MQnf If you specified a log_file in the
FirefoxBinary constructor, check it for details.
I have Firefox49 on Ubuntu 14.04 LTS. I had selenium 2.53.6 and reading a previous post, I upgraded to selenium 3.0.0.b3. I also downloaded geckdriver and put it in /usr/bin
It looks like I was still running older version of selenium. But when I uninstalled that and installed selenium 3.0.0.b3, I see the following error -
selenium.‌​common.exceptions.We‌​bDriverException:
Message: Service geckodriver unexpectedly exited. Status code was: 2
What am I missing?
Try renaming the downloaded Gecko Driver to Wires and Set the Capabilities as mentioned Below.
System.setProperty("webdriver.gecko.driver", "//home//.....//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
Driver = new MarionetteDriver(capabilities);
I tried with the Above code on FireFox 49 on Linux Ubuntu 14.04 LTS... Code Works Fine For Me in Java..
Also try to downgrade the selenium WebDriver from Beta to 2.53 as Beta version is unstable..

Headless Selenium + Xvfb + Chrome on OSX 10.11

Okay, so first I learned that Xvfb wasn't included with my OS X version, so I installed it from http://www.xquartz.org/.
and that seemed to have worked:
which xvfb
/opt/X11/bin/xvfb
But when I try using it with either pyvirtualdisplay and xvfbwrapper, following advice I found on this question How do I run Selenium in Xvfb? My script runs without errors but just opens in a Chrome browser window:
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()
browser.get('google.com')
Am I doing something wrong here?
I believe that Chrome is built for Quartz ui framework, so it ignores the X11 windowing engine. You will need to install an X11 version of a browser and then execute that.
For me, this code works fine on OSX 10.13. You don't need pyvirtualdisplay, because you can run chrome in headless mode. Just download chromedriver that fits to your chrome version and put it to usr/local/bin
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--mute-audio')
options.add_argument('--lang=de')
options.add_argument('--window-size=800,600')
options.add_argument('--disable-notifications')
options.add_argument('--enable-popup-blocking')
browser = webdriver.Chrome(chrome_options=options, executable_path='/usr/local/bin/chromedriver')
browser.get('some url')

Unsupported command-line flag: --ignore-certificate-errors

Using Python 2.7.5, python module selenium (2.41.0) and chromedriver (2.9).
When Chrome starts it displays a message in a yellow popup bar: "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." This simple example reproduces the problem.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://google.com/")
How do I remove this command-line flag in python selenium?
This extra code removes the --ignore-certificate-errors command-line flag for me. In my opinion the arguments that can be added to webdriver.Chrome() could (and should) be better documented somewhere, I found this solution in a comment on the chromedriver issues page (see post #25).
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
browser = webdriver.Chrome(chrome_options=options)
browser.get("http://google.com/")
This issue is resolved as of Chromedriver 2.11 (released Oct 2014). Updating will now do the trick.
you can use the following flag --test-type
var options = new ChromeOptions();
options.AddArguments(new[] {
"--start-maximized",
"allow-running-insecure-content",
"--test-type" });
return new ChromeDriver(options);
This is what I'm currently using in Java to get around this issue but I don't know how Python works but worth a try anyway
ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, chrome);
capabilities.setCapability("chrome.binary",
"C:\\set path to driver here\\chromedriver.exe");
options = webdriver.ChromeOptions()
options.add_argument('test-type')
chromedriver = 'resources/chromedriver.exe'
os.environ["webdriver.chrome.driver"] = chromedriver
self.driver = webdriver.Chrome(chromedriver,chrome_options=options)
I was having this problem using Selenium2 with Robot on a Mac. The problem ended up being that I had the wrong version of chromedriver installed on my system...
$ chromedriver
Starting ChromeDriver (v2.9.248307) on port 9515 <<Version 2.9 was the problem
I found it in /usr/local/bin and just removed it and replaced it from the official download page and it seems to have cleared it all up...
$ chromedriver
Starting ChromeDriver 2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1) on port 9515
Only local connections are allowed.

Categories