首先,机器和软件规格: 我正在运行:
ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
我正在尝试通过Selenium运行Chrome浏览器。我发现了这些命令:这些, 可以通过Google Chrome使用Selenium。
我有一个测试目录,里面只有chromedriver二进制文件和脚本。目录的位置是:/home/kela/test_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
有人能解释一下为什么我需要使用capabilities,而其他人不需要就可以运行相同的脚本吗?我尝试过添加:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
但我得到了相同的错误。所以,我不确定我需要添加什么能力(考虑到其他人没有问题)?
编辑1:回应DebanjanB下面的评论:
Chromedriver在预期位置。我使用的是Windows 10。从这里,预期位置是C:\Program Files (x86)\Google\Chrome\Application\chrome.exe;这就是我的机器上它的位置(我从chrome属性表中复制并粘贴了此位置)。
ChromeDriver对非根用户具有可执行权限。
我肯定已经安装了Google Chrome v75.0(我可以看到产品版本为75.0.3770.100)
我以非root用户身份运行脚本,因为我的bash命令行以$结尾而不是#(即kela:〜/test_dir $而不是kela:〜/test_dir#)
编辑2:根据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只是“data:,”。如果我从脚本中删除“driver.get('http://google.com')”这一行,同样的事情也会发生,所以我知道警告/问题出在这一行上:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
例如,从这里,我尝试添加:
options.add_argument('--profile-directory=Default')
但是出现了相同的警告。
编辑3:
由于编辑3开始偏离了这里具体讨论的问题,我在这里开了一个新问题here。