1.正常调用时存在的问题
import selenium.webdriver
from selenium import webdriver
browser=webdriver.Firefox()
selenium.common.exceptions.SessionNotCreatedException: 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
在通过https://github.com/mozilla/geckodriver/releases/该网址进行下载合适的firefox driver 版本后,将该文件移动到程序所在目录时仍不能解决该问题。
2.通过引入binary来尝试解决问题。
import selenium.webdriver
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r'E:\firefox\firefox.exe')
browers = webdriver.Firefox(firefox_binary=binary)
在运行后出现以下问题:
DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
browers = webdriver.Firefox(firefox_binary=binary)
说是firefox已经弃用该服务了,于是通过引入options来解决。(该问题并不影响正常程序运行和服务)
3.使用service和options来尝试解决问题
import selenium.webdriver
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
driver_path=(r'E:\firefox\geckodriver.exe')#driver_path就是你的驱动路径
binary_path=(r'E:\firefox\firefox.exe') #binary_path就是你的游览器路径
ops=Options()
ops.binary_location=binary_path
serve=Service(driver_path)
browers=selenium.webdriver.Firefox(service=serve,options=ops)
可以正常运行该程序,问题解决