我已经下载了最新版本的Firefox和相应的(64位)geckodriver。将geckodriver可执行文件的位置添加到PATH。但是,当我执行以下代码时:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
要调用Firefox,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Karun\Documents\NJCC all data at once Selenium.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:\Users\Karun\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 162, in __init__
keep_alive=True)
File "C:\Users\Karun\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\Karun\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Karun\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\Karun\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: permission denied
您看到的错误确实给我们提示了可能出现的问题,如下所示:
Traceback (most recent call last):
File "C:\Users\Karun\Documents\NJCC all data at once Selenium.py", line 2, in <module>
browser = webdriver.Firefox()
selenium.common.exceptions.SessionNotCreatedException: Message: permission denied
虽然您提到您已将geckodriver可执行文件的位置添加到PATH但仍面临上述错误,因此一个简单的解决方案是从mozilla/geckodriver下载最新的GeckoDriver并传递Key executable_path
以及绝对路径的值初始化webdriver和Web Browser实例时的GeckoDriver二进制文件如下:
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://www.google.com")
print("Page Title is : %s" %driver.title)
driver.quit()
另外确保您使用: