简体   繁体   中英

Selenium Geckodriver and ChromeDriver not working

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.

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

Regards, https://github.com/ultimosparc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM