Per this previous question I updated Selenium to version 2.0.1
But now I have another error, even when the profile files exist under /tmp/webdriver-py-profilecopy:
File "/home/sultan/Repository/Django/monitor/app/request.py", line 236, in perform
browser = Firefox(profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 46, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 46, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 44, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 87, in _wait_until_connectable
raise WebDriverException("Can't load the profile. Profile Dir : %s" % self.profile.path)
selenium.common.exceptions.WebDriverException: Can't load the profile. Profile Dir : /tmp/webdriver-py-profilecopy
Selenium team fixed in latest version. For almost all environments the fix is:
pip install -U selenium
Unclear at which version it was fixed (apparently r13122), but certainly by 2.26.0 (current at time of update) it is fixed.
This error means that _wait_until_connectable is timing out, because for some reason, the code cannot connect to the webdriver extension that has been loaded into the firefox.
I have just reported an error to selenium where I am getting this error because I’m trying to use a proxy and only 2 of the 4 configured changes in the profile have been accepted by firefox, so the proxy isn’t configured to talk to the extension. Not sure why this is happening…
I had the same issue after upgrading Ubuntu to 12.04.
The issue was on the package side and has been fixed in the latest version of the library. Just update the selenium library. For almost all Python environments this is:
#Download version 30 for Linux (This is the 64 bit)
wget http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/30.0/linux-x86_64/en-US/firefox-30.0.tar.bz2
tar -xjvf firefox-30.0.tar.bz2
#Remove the old version
sudo rm -rf /opt/firefox*
sudo mv firefox /opt/firefox30.0#Create a permanent link
sudo ln -sf /opt/firefox30.0/firefox /usr/bin/firefox
I faced the same problem with FF 32.0 and Selenium selenium-2.42.1-py2.7.egg. Tried to update selenium, but it is already the latest version.
The solution was to downgrade Firefox to version 30. Here is the process:
#Download version 30 for Linux (This is the 64 bit)
wget http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/30.0/linux-x86_64/en-US/firefox-30.0.tar.bz2
tar -xjvf firefox-30.0.tar.bz2
#Remove the old version
sudo rm -rf /opt/firefox*
sudo mv firefox /opt/firefox30.0
#Create a permanent link
sudo ln -sf /opt/firefox30.0/firefox /usr/bin/firefox
This solved all the problems, and this combination works better !
classCygwinFirefoxProfile(FirefoxProfile):@propertydef path(self):
path = self.profile_dir
# Do stuff to the path as described in Jeff Hoye's answerreturn path
As an extension to Jeff Hoye‘s answer, a more ‘Pythonic’ way would be to subclass webdriver.firefox.firefox_profile.FirefoxProfile as follows:
class CygwinFirefoxProfile(FirefoxProfile):
@property
def path(self):
path = self.profile_dir
# Do stuff to the path as described in Jeff Hoye's answer
return path
If you are running webdriver from cygwin, the problem is that the path to the profile is still in POSIX format which confuses windows programs. My solution uses cygpath to convert it into Windows format.
in this file/method:
selenium.webdriver.firefox.firefox_binary.launch_browser():
Since Python is not even close to my primary programming language, if someone can recommend a more pythonic approach maybe we can push it into the distribution. It sure would be handy if it worked in cygwin right out of the box.
I had the same problem and believed it was the wrong combo of selenium / Firefox. Turned out that my .mozilla/ folder permissions were only accessible to the root user. Doing chmod 770 ~/.mozilla/ did the trick. I would suggest making sure this is not the issue before troubleshooting further.
I had this same issue with Firefox 34.0.5 (Dec 1, 2014) and upgrading Selenium from 2.42.1 to 2.44.0 resolved my issue.
However, I’ve have since seen this issue again, I think with 2.44.0, and another upgrade fixed it. So I’m wondering if it might be fixed by simply uninstalling and then re-installing. If so, I’m not sure what that would indicate the underlying problem is.
I was using selenium 2.53 and firefox version 55.0. I solved this issue by installing the older version of firefox (46.0.1) since selenium 2.53 will not work for firefox version 47.0 & above.
This is not a proper solution but worked for me, if somebody can improve I would be glad to know. I just run my script as root: sudo python myscript.py. I guess I can solve by changing the profile default file or directory could work.
from selenium import webdriver
browser = webdriver.Firefox()
exceptions:-
Exception ignored in:<bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>Traceback(most recent call last):File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163,in __del__
self.stop()File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135,in stop
if self.process isNone:AttributeError:'Service' object has no attribute 'process'Exception ignored in:<bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>Traceback(most recent call last):File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163,in __del__
self.stop()File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135,in stop
if self.process isNone:AttributeError:'Service' object has no attribute 'process'Traceback(most recent call last):File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64,in start
stdout=self.log_file, stderr=self.log_file)File"C:\Python\Python35\lib\subprocess.py", line 947,in __init__
restore_signals, start_new_session)File"C:\Python\Python35\lib\subprocess.py", line 1224,in _execute_child
startupinfo)FileNotFoundError:[WinError2]The system cannot find the file specified
During handling of the above exception, another exception occurred:Traceback(most recent call last):File"<pyshell#11>", line 1,in<module>
browser = webdriver.Firefox()File"C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135,in __init__
self.service.start()File"C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71,in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException:Message:'geckodriver' executable needs to be in PATH.
I’m new to programming and started with Python about 2 months ago and am going over Sweigart’s Automate the Boring Stuff with Python text. I’m using IDLE and already installed the selenium module and the Firefox browser.
Whenever I tried to run the webdriver function, I get this:
from selenium import webdriver
browser = webdriver.Firefox()
Exception :-
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I think I need to set the path for geckodriver but not sure how, so can anyone tell me how would I do this?
Actually The Selenium client bindings tries to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.
On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell:
On Windows you will need to update the Path system variable to add the full directory path to the executable geckodrivermanually or command line(don’t forget to restart your system after adding executable geckodriver into system PATH to take effect). The principle is the same as on Unix.
Now you can run your code same as you’re doing as below :-
from selenium import webdriver
browser = webdriver.Firefox()
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line
Exception clearly states you have installed firefox some other location while Selenium is trying to find firefox and launch from default location but it couldn’t find. You need to provide explicitly firefox installed binary location to launch firefox as below :-
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)
回答 1
这为我解决了。
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
The answer by @saurabh solves the issue, but doesn’t explain why Automate the Boring Stuff with Python doesn’t include those steps.
This is caused by the book being based on selenium 2.x and the Firefox driver for that series does not need the gecko driver. The Gecko interface to drive the browser was not available when selenium was being developed.
The latest version in the selenium 2.x series is 2.53.6 (see e.g this answers, for an easier view of the versions).
The 2.53.6 version page doesn’t mention gecko at all. But since version 3.0.2 the documentation explicitly states you need to install the gecko driver.
If after an upgrade (or install on a new system), your software that worked fine before (or on your old system) doesn’t work anymore and you are in a hurry, pin the selenium version in your virtualenv by doing
pip install selenium==2.53.6
but of course the long term solution for development is to setup a new virtualenv with the latest version of selenium, install the gecko driver and test if everything still works as expected. But the major version bump might introduce other API changes that are not covered by your book, so you might want to stick with the older selenium, until you are confident enough that you can fix any discrepancies between the selenium2 and selenium3 API yourself.
from selenium import webdriver
from webdriver_manager.firefox importGeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
The easiest way for windows!
Download the latest version of geckodriver from here. Add the geckodriver.exe file to the python directory (or any other directory which already in PATH). This should solve the problem (Tested on Windows 10)
By this you are appending the path to GeckoDriver to your System PATH. This tells the system where GeckoDriver is located when executing your Selenium scripts.
4) Save the .bash_profile and force it to execute. This loads the values immediately without having to reboot. To do this you can run the following command:
source ~/.bash_profile
5) That’s it. You are DONE!. You can run the Python script now.
Some additional input/clarification for future readers of this thread:
The following suffices as a resolution for Windows 7, Python 3.6, selenium 3.11:
@dsalaj’s note in this thread earlier for Unix is applicable to Windows as well; tinkering with the PATH env. variable at the Windows level and restart of the Windows system can be avoided.
(1) Download geckodriver (as described in this thread earlier) and place the (unzipped) geckdriver.exe at X:\Folder\of\your\choice
(2) Python code sample:
import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';
from selenium import webdriver;
browser = webdriver.Firefox();
browser.get('http://localhost:8000')
assert 'Django' in browser.title
Notes:
(1) It may take about 10 seconds for the above code to open up the Firefox browser for the specified url.
(2) The python console would show the following error if there’s no server already running at the specified url or serving a page with the title containing the string ‘Django’:
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//localhost%3A8000/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%9
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities importDesiredCapabilities
firefox_capabilities =DesiredCapabilities.FIREFOX
firefox_capabilities['marionette']=True#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts']=True
firefox_capabilities['acceptSslCerts']=True
firefox_capabilities['acceptInsecureCerts']=True#In the next line I'm using a specific FireFox profile because# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver# I create a FireFox profile where I had already made an exception for the site I'm testing# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
ffProfilePath ='D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath ='D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
I’m running a VirtualEnv (which I manage using PyCharm, I assume it uses Pip to install everything)
In the following code I can use a specific path for the geckodriver using the executable_path paramater (I discoverd this by having a look in
Lib\site-packages\selenium\webdriver\firefox\webdriver.py ). Note I have a suspicion that the order of parameter arguments when calling the webdriver is important, which is why the executable_path is last in my code (2nd last line off to the far right)
AFter investigation it was found that the Marionette driver is incomplete and still in progress, and no amount of setting various capabilities or profile options for dismissing or setting certifcates was going to work. So it was just easier to use a custom profile.
Anyway here’s the code on how I got the geckodriver to work without being in the path:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True
#In the next line I'm using a specific FireFox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a FireFox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
It’s really rather sad that none of the books published on Selenium/Python and most of the comments on this issue via Google do not clearly explain the pathing logic to set this up on Mac (everything is Windows!!!!). The youtubes all pickup at the “after” you’ve got the pathing setup (in my mind, the cheap way out!). So, for you wonderful Mac users, use the following to edit your bash path files:
>$touch ~/.bash_profile; open ~/.bash_profile
Then add a path something like this….
*# Setting PATH for geckodriver
PATH=“/usr/bin/geckodriver:${PATH}”
export PATH
This worked for me. My concern is when will the Selenium Windows community start playing the real game and include us Mac users into their arrogant club membership.
回答 16
硒在他们的DESCRIPTION.rst中回答了这个问题
Drivers=======Selenium requires a driver to interface with the chosen browser.Firefox,for example, requires `geckodriver <https://github.com/mozilla/geckodriver/releases>`_, which needs to be installed before the below examples can be run.Make sure it's in your `PATH`, e. g., place it in `/usr/bin` or `/usr/local/bin`.
Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Selenium answers this question in their DESCRIPTION.rst
Drivers
=======
Selenium requires a driver to interface with the chosen browser. Firefox,
for example, requires `geckodriver <https://github.com/mozilla/geckodriver/releases>`_, which needs to be installed before the below examples can be run. Make sure it's in your `PATH`, e. g., place it in `/usr/bin` or `/usr/local/bin`.
Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Basically just download the geckodriver, unpack it and move the executable to your /usr/bin folder
回答 17
对于Windows用户
使用原始代码:
from selenium import webdriver
browser = webdriver.Firefox()
driver.get("https://www.google.com")
If you use virtual environment and win10(maybe it’s the for other systems), you just need to put geckodriver.exe into the following folder in your virtual environment directory:
from webdriverdownloader importGeckoDriverDownloader# vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd =GeckoDriverDownloader()
gdd.download_and_install()#gdd.download_and_install("v0.19.0")
这将为您提供Windows上gekodriver.exe的路径
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.com/')
from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd = GeckoDriverDownloader()
gdd.download_and_install()
#gdd.download_and_install("v0.19.0")
this will get you the path to your gekodriver.exe on windows
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.com/')
I am using Windows 10 and Anaconda2. I tried setting system path variable but didn’t worked out. Then I simply added geckodriver.exe file to Anaconda2/Scripts folder and everything works great now.
For me the path was:-
To add my 5 cents, it is also possible to do echo PATH (Linux) and just move geckodriver to the folder of your liking. If a system (not virtual environment) folder is the target, the driver becomes globally accessible.