[英]Selenium Geckodriver and ChromeDriver not working
Running OS 10.12.6 Selenium with Python 3.6 bindings使用 Python 3.6 绑定运行 OS 10.12.6 Selenium
Despite my best efforts I can't seem to get either working with Selenium.尽管我尽了最大的努力,但我似乎无法与 Selenium 一起工作。 Here's the error I get:
这是我得到的错误:
Geckodriver error: Geckodriver 错误:
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. chromedriver 和 geckodriver 的错误基本相同。
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.
我已经删除了我下载的两个版本,并通过 Homebrew 安装了两个驱动程序。 Same errors.
同样的错误。 I'm not sure what to do next.
我不知道接下来要做什么。
Homebrew needed to link the drivers. Homebrew 需要连接驱动程序。 Once this was done they worked perfectly.
一旦完成,他们就完美地工作了。
I am able to use chromedriver now without a problem.我现在可以毫无问题地使用 chromedriver。 I set chromedriver.exe in a file folder renamed chromedriver, then I make sure the filepath is in parentheses.
我将 chromedriver.exe 设置在重命名为 chromedriver 的文件夹中,然后确保文件路径在括号中。 I however have the same problem with opening geckodriver through selenium despite the fact that pathlib.Path recognizes that the geckodriver.exe file exists.
然而,尽管 pathlib.Path 识别出 geckodriver.exe 文件存在,但我在通过 selenium 打开 geckodriver 时遇到了同样的问题。 See if you can get chromedriver working this way:
看看你是否可以让 chromedriver 以这种方式工作:
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).与上面的最后一行一样,您应该确保以原始字符串的形式包含文件路径(原始字符串确保 Python 不会将反斜杠读取为转义字符)。 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.
如果您确实想出如何使用 geckodriver 解决您的问题(因为我已尝试通过高级设置将其添加到系统路径中但它不起作用),请告诉我,但这应该可以让 Selenium 与 Chrome 一起工作。
Hi I have created new bash script which installs automatically geckodriver, firefox, python, selenium and tests the configuration.嗨,我创建了新的 bash 脚本,它会自动安装 geckodriver、firefox、python、selenium 并测试配置。
#!/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问候, https://github.com/ultimosparc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.