使用Selenium执行Python脚本时发生错误

时间:2018-11-13 16:44:43

标签: python selenium selenium-webdriver selenium-chromedriver

我已经在系统中安装了python,pip和selenium,并且正在基本站点上测试示例代码。

代码:

from selenium import webdriver
import time


driver = webdriver.Chrome("C:\\Users\\skandregula\\PycharmProjects\\sele\\geckodriver.exe")
driver.get("http://www.python.org")

time.sleep(10)
driver.close()
error:C:\Users\skandregula\PycharmProjects\sele\venv\Scripts\python.exe
C:/Users/skandregula/PycharmProjects/sele/script.py Traceback (most
recent call last):   File
"C:/Users/skandregula/PycharmProjects/sele/script.py", line 5, in
<module>
    driver = webdriver.Chrome("C:\\Users\\skandregula\\PycharmProjects\\sele\\geckodriver.exe")
File
"C:\Users\skandregula\PycharmProjects\sele\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 81, in __init__
    desired_capabilities=desired_capabilities)   File "C:\Users\skandregula\PycharmProjects\sele\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 157, in __init__
    self.start_session(capabilities, browser_profile)   File "C:\Users\skandregula\PycharmProjects\sele\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)   File "C:\Users\skandregula\PycharmProjects\sele\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 321, in execute
    self.error_handler.check_response(response)   File "C:\Users\skandregula\PycharmProjects\sele\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 242, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: Unable
to find a matching set of capabilities

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

driver = webdriver.Chrome("C:\\Users\\skandregula\\PycharmProjects\\sele\\geckodriver.exe")

此行尝试启动Chrome浏览器的实例,但是您将其传递到geckodriver.exe的路径,chromedriver.exe是Firefox的Webdriver。您需要在Chrome上使用CNAME