selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:没有与ChromeDriver Chrome selenium匹配的功能错误
首先,机器和包装规格: 我正在跑步:selenium.common.exceptions.SessionNotCreatedException:消息:未创建会话:没有与ChromeDriver Chrome selenium匹配的功能错误,selenium,google-chrome,selenium-webdriver,webdriver,selenium-chromedriver,Selenium,Google Chrome,Selenium Webdriver,Webdriver,Selenium Chromedriver,首先,机器和包装规格: 我正在跑步: ChromeDriver version 75.0.3770.140 Selenium: version '3.141.0' WSL (linux subsystem) of windows 10 我正在尝试通过selenium运行chromebrowser。我发现:命令,通过谷歌浏览器使用硒 我有一个测试目录,其中只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test\u dir/ 我运行了代码: import sel
ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
我正在尝试通过selenium运行chromebrowser。我发现:命令,通过谷歌浏览器使用硒
我有一个测试目录,其中只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test\u dir/
我运行了代码:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
options = Options()
options.binary_location='/home/kela/test_dir/chromedriver'
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
此代码的输出为:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
有人能解释一下,当同一个脚本适用于没有功能的其他脚本时,我为什么需要功能吗?我确实尝试添加:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
但我也犯了同样的错误。因此,我不确定我需要添加哪些功能(考虑到它适用于没有它的其他人?)
编辑1:处理DebanjanB的评论如下:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location='/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
driver = webdriver.Chrome(options=options)
driver.get('http://google.com/')
生成一个对话框,内容如下:
Google Chrome无法读取和写入其数据目录:/tmp/.com/Google.Chrom.gyw63s
因此,我再次检查了我的Chrome权限,我应该能够写入Chrome:
另外,我可以看到/tmp/中有一堆.com目录:
.com.google.Chrome.4jnWme/ .com.google.Chrome.FdNyKP/ .com.google.Chrome.VAcWMQ/ .com.google.Chrome.ZbkRx0/ .com.google.Chrome.iRrceF/
.com.google.Chrome.A2QHHB/ .com.google.Chrome.G7Y51c/ .com.google.Chrome.WD8BtK/ .com.google.Chrome.cItmhA/ .com.google.Chrome.pm28hN/
然而,由于这似乎更多的是一个警告而不是一个错误,我点击“确定”关闭对话框,一个新的选项卡在浏览器中打开;但URL只是“数据:,”。如果我从脚本中删除行“driver.get(“”)”,则会发生同样的情况,因此我知道警告/问题与该行有关:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
例如,从中,我尝试添加:
options.add_argument('--profile-directory=Default')
但同样的警告也出现了
编辑3:
当编辑3开始转向一个与此处具体讨论的问题不同的问题时,我开始了一个新问题。此错误消息
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
…意味着ChromeDriver无法启动/生成新的网络浏览器,即Chrome浏览器会话
二进制位置 设置/获取Chrome(可执行)二进制文件的位置,并定义为:
def binary_location(self, value):
"""
Allows you to set where the chromium binary lives
:Args:
- value: path to the Chromium binary
"""
self._binary_location = value
因此,根据您的代码试用,options.binary\u location='/home/kela/test\u dir/chromedriver'
是不正确的
解决方案 如果Chrome安装在默认位置,则可以安全地删除此属性。如果Chrome安装在自定义位置,则需要使用
选项.binary\u location
属性来指向Chrome安装
您可以在中找到详细的讨论
实际上,您的代码块将是:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(options=options, executable_path='/home/kela/test_dir/chromedriver.exe')
driver.get('http://google.com/')
此外,请确保:
- ChromeDriver对非root用户具有可执行权限
- 当您使用ChromeDriver v75.0时,请确保您的Google Chrome v75.0推荐版本为:
- 以非root用户身份执行Selenium测试
---------ChromeDriver 75.0.3770.8 (2019-04-29)---------
Supports Chrome version 75