Running OS 10.12.6 Selenium with Python 3.6 bindings
Despite my best efforts I can't seem to get either working with Selenium. Here's the error I get:
Geckodriver error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/christopher.gaboury/Desktop/Scripts/safariExecutive.py", line 11, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 148, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
The error for both chromedriver and geckodriver are essentially the same.
I've manually set my path to where these are located. Same error. I've moved the drivers to a location already in the path. Same error. Ive removed the two versions I downloaded and installed both drivers via Homebrew. Same errors. I'm not sure what to do next.
Homebrew needed to link the drivers. Once this was done they worked perfectly.
I am able to use chromedriver now without a problem. I set chromedriver.exe in a file folder renamed chromedriver, then I make sure the filepath is in parentheses. I however have the same problem with opening geckodriver through selenium despite the fact that pathlib.Path recognizes that the geckodriver.exe file exists. See if you can get chromedriver working this way:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser=webdriver.Chrome(r'c:\path_to_chromedriver\chromedriver\chromedriver.exe')
As in the last line above, you should make certain that you include the file path in the form of a raw string (the raw string making sure that Python doesn't read the backslashes as escape characters). If you do ever figure out how to solve your problem with geckodriver (as I have tried adding it to the system Path through advanced settings yet it didn't work), please let me know, but that should get Selenium working with Chrome.
I have created new bash script which installs automatically geckodriver, firefox, python, selenium and tests the configuration.
#!/bin/bash
apt update && apt install -y firefox python3 python3-pip
pip install selenium
INSTALL_DIR="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
input=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
urls=$(echo $input | tr " " "\n")
for addr in $urls
do
url=$addr
break
done
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$INSTALL_DIR"
echo "installed geckodriver binary in $INSTALL_DIR"
python3 -c "from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get(\"http://google.com/\")
print (\"Headless Firefox Initialized\")
driver.quit()"
Related
I've upgraded Ubuntu to 20.04. It seems that Chrome isn't available as APT package but via snap. After the upgrade I'm getting the error while trying to instantiate Chrome browser:
>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.headless = True
>>> o.add_argument('--disable-dev-shm-usage')
>>> o.add_argument('--no-sandbox')
>>> Chrome(options=o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
chromedriver version is 87.0.4280.20
chromium-browser version is Chromium 87.0.4280.66 snap
I read this discussion. The case is however isn't same. I run python as a regular user. However I've disabled dev-shm-usage and sandbox. But still it doesn't work. It worked before I've upgraded to Ubuntu 20.04. So I assume it has something to do with snap version of Chrome.
I have found out following configuration works:
Start first Chrome chromium-browser --headless --remote-debugging-port=9222 and then:
>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.add_experimental_option('debuggerAddress', 'localhost:9222')
>>> b = Chrome(options=o)
>>> b.get('https://google.com')
>>> b.title
'Google'
>>>
So it seems the problem is with starting the browser.
Opened a bug report
Good explanation was provided by ChromeDriver development team in a response to the ticket I've submitted:
ChromeDriver uses the /tmp directory to communicate with Chromium, but
Snap remaps /tmp directory to a different location (specifically, to
/tmp/snap.chomium/tmp). This causes errors because ChromeDriver can't
find files created by Chromium. ChromeDriver is designed and tested
with Google Chrome, and it may have compatibility issues with
third-party distributions.
There are a couple of workarounds:
Tell ChromeDriver to use a location in your home directory, instead of /tmp. For example, if your home directory is /home/me, you can add
the following line of code to your script:
o.add_argument('--user-data-dir=/home/me/foo')
Explicitly select a port for ChromeDriver to communiate with Chromium. You need to carefully select a port (e.g., 9222) that isn't
already in use, and then add the following line to your script:
o.add_argument('--remote-debugging-port=9222')
and
Another (probably better) workaround is to use the ChromeDriver
installed by Snap, which is at /snap/bin/chromium.chromedriver. E.g.,
driver = Chrome('/snap/bin/chromium.chromedriver', options=o)
Since this version of ChromeDriver runs inside Snap, its /tmp
directory is redirected in the same way as Chromium
The solution to my problem is rather workaround but nevertheless it might be useful.
I have downgraded to Chrome version 86, which I have installed from deb package instead of snap. Installation instructions can be found here
Once I've downgraded chromium and chromedriver to version 86 the problem has gone.
The answer provided by Ralfeus is wholesome, I am using Ubuntu 20.04 and was facing same issues.
I used driver = Chrome('/snap/bin/chromium.chromedriver') , yeah without any additional options and it worked.
im using pycharm and my pythn
version 3.6.7 pip 9.0.1
and selenium version selenium-3.141.0 urllib3-1.24.1
i install selenium using this commands
pip3 install selenium
then i code like this
from selenium import webdriver
driver = webdriver.Firefox("/home/ghost/automation/pwd/geckodriver")
driver.set_page_load_timeout(30)
driver.get("https://www.google.com/")
driver.maximize_window()
driver.implicitly_wait(120)
driver.get_screenshot_as_file("google.png")
driver.quit()
**when i run this i get this error **
/home/ghost/PycharmProjects/try/venv/bin/python /home/ghost/PycharmProjects/try/open/testcas1.py
Traceback (most recent call last):
File "/home/ghost/PycharmProjects/try/open/testcas1.py", line 3, in <module>
driver = webdriver.Firefox("/home/ghost/automation/pwd/geckodriver")
File "/home/ghost/PycharmProjects/try/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
firefox_profile = FirefoxProfile(firefox_profile)
File "/home/ghost/PycharmProjects/try/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 80, in __init__
ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
File "/usr/lib/python3.6/shutil.py", line 309, in copytree
names = os.listdir(src)
NotADirectoryError: [Errno 20] Not a directory: '/home/ghost/automation/pwd/geckodriver'
Process finished with exit code 1
and in this line driver = webdriver.Firefox("/home/ghost/automation/pwd/geckodriver") its correct path of my geckodriver nd my geckodriver
version is 0.23.0
these answers are not help to me
https://stackoverflow.com/a/40399367/8337986
https://stackoverflow.com/a/42945346/8337986
In Brief
Need to use the param key executable_path
In Details
While working with GeckoDriver, Firefox and Selenium, you need to use the Key executable_path and the Value set to the absolute path of the GeckoDriver within single quotes i.e. '...' with forward slash i.e. / as path separator as follows:
driver = webdriver.Firefox(executable_path='/home/ghost/automation/pwd/geckodriver')
or use default location
driver = webdriver.Firefox(executable_path=GeckoDriverManager(cache_valid_range=1).install())
I just put the geckodriver.exe file to my main project folder and identified link as:
driver=webdriver.Firefox("D:\Desktop\project") without geckodriver, everything worked.
try whith
"/home/ghost/automation/pwd"
or
"\\home\\ghost\\automation\\pwd",
"\home\ghost\automation\pwd"
if you using Atom IDE maybe your problem is you run a .py file with a script package. It's working not same as cmd console
I'm using Debian 9 Stretch and Pycharm IDE and trying to learn web-scramping; I installed the Selenium package simply by running:
pip install selenium
and the Firefox webdriver by running:
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -xvzf geckodriver-v0.19.1-linux64.tar.gz.1
chmod +x geckodriver
respectively, to download the last release, extract it and make the driver executable. After that, I added the driver to the following path:
usr/local/bin
I ran all by using the Pycharm IDE terminal and not the built-in Debian terminal.
In order to open Firefox and web-scrape, I run:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
The last line gives an error message as output:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
I'm a newbie both in Python and in web-scraping; please, could someone explain what does go wrong with installation and coding and why I got this error?
In the hope to be clear asking the question, I thanks all in advance for the help.
Assuming that the location of the geckodriver is correct, you can check below:
properties of the geckodriver should have the correct permission for the user. You would need to check the box "Allow this file to run as a program" or
if you you have restricted access, save the geckodriver in your home/username/geckodriver then path it to your firefox. Saving it in your home folder will be able to modify the properties of your geckodriver.
[EDIT] Are your running in command line? If so, you need a virtual display, I have used pyvirtualdisplay:
from pyvirtualdisplay import Display
display = Display(visible=0,size(800, 600))
display.start()
driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
The problem has been solved by removing Mozilla Firefox, that, in Debian 9 Stretch is installed as ESR (Extended Release Support) by default; at the time the Firefox ESR version was 52.0.
After, I installed the by instaling the unstable Firefox version (not Beta) by running on the the terminal as super-user:
su -
gedit /etc/apt/sources.list
and adding deb http://ftp.it.debian.org/debian/ unstable main to the sources list file.
After, I ran:
apt-get update
apt-get install -t unstable firefox
to update the software and install Firefox.
By following the guidelines explained in the question to install and run the selenium Python package everything should work fine (at least, for me!).
Hope this will help other users too!
While woking with Selenium-Python Client v3.10.0 along with GeckoDriver v0.19.1 and Firefox v58.0.2 you have to initialize the WebDriver instance and assign it to a variable, which will in-turn initialize the Web Browser which will in-turn open the desired URL as follows :
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
Problem
I'm following of the examples listed on the documentation here. Basically, just trying to get Selenium to open Firefox, take me to a site, make a query and then close the browser.
I've been running into an issue where Selenium will open the browser, but then not go to the specified website. In other cases, I receive an error, I should mention this error only appear after upgrading Selenium.
Traceback (most recent call last):
File "script.py", line 4, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 142, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
I'm using the latest version of Firefox, 54.0.1
I'm using Python version 2.7.11
And I have Selenium version 3.4.3
script.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.yahoo.com')
assert 'Yahoo!' in browser.title
elem = browser.find_element_by_name('p') # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)
browser.quit()
In this case, these were the steps I took to solve this issue.
Updated Firefox to the latest version
Updated Selenium as well
Removed incorrect version of geckodriver from bin folder
Updated xcode in App Store
Used brew install geckodriver to get correct version into bin folder
Used brew link
Ran script.py
I have read previous questions asked on this topic and tried to follow the suggestions but I continue to get errors. On terminal, I ran
export PATH=$PATH:/Users/Conger/Documents/geckodriver-0.8.0-OSX
I also tried
export PATH=$PATH:/Users/Conger/Documents/geckodriver
When I run the following Python code
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/Users/Conger/Documents/Firefox.app'
driver = webdriver.Firefox(capabilities=firefox_capabilities)
I still get the following error
Python - testwebscrap.py:8
Traceback (most recent call last):
File "/Users/Conger/Documents/Python/Crash_Course/testwebscrap.py", line 11, in <module>
driver = webdriver.Firefox(capabilities=firefox_capabilities)
File "/Users/Conger/miniconda2/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/Users/Conger/miniconda2/lib/python2.7/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.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1006df6d0>> ignored
[Finished in 0.194s]
you may downgrade your selenium by
pip install selenium==2.53.6
This has solved my issue
On mac:
brew install geckodriver
Homebrew is the most popular package manager for Mac OS X, you will need install XCode on your mac and it will be then accesible from your terminal.
You can follow this tutorial if required
First we know that gekodriver is the driver engine of Firefox,and we know that
driver.Firefox() is used to open Firefox browser, and it will call the gekodriver engine ,so we need to give the gekodirver a executable permission.
so we download the latest gekodriver uncompress the tar packge ,and put gekodriver at the /usr/bin/
ok,that's my answer and i have tested.
I just downloaded the latest version geckodriver (I have win7) from here and added that exe-file in python directory (which already in PATH)
export path works only in the terminal you have entered the command. If you try to run the script from a different terminal, you will get the same error.