【发布时间】:2019-11-27 06:46:54
【问题描述】:
首先,机器和包装规格: 我在跑步:
ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
我正在尝试通过 selenium 运行 chromebrowser。我发现:these 命令,通过谷歌浏览器使用硒。
我有一个测试目录,其中只有 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
当同一个脚本适用于其他没有能力的人时,谁能解释为什么我需要能力?我确实尝试添加:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
但我遇到了同样的错误。所以我不确定我需要添加哪些功能(考虑到没有它的其他人也可以使用它?)
编辑 1:在下面解决 DebanjanB 的 cmets:
Chrome 驱动程序位于预期位置。我正在使用 Windows 10。来自here,预期位置是 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe;这就是它在我机器上的位置(我从 chrome 属性表中复制并粘贴了这个位置)。
ChromeDriver 对非 root 用户具有可执行权限。
我肯定安装了 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 cannot read and write to it's data directory: /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('http://google.com')' 行,也会发生同样的事情,所以我知道警告/问题出在以下行:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
例如,从here,我尝试添加:
options.add_argument('--profile-directory=Default')
但同样的警告会弹出。
编辑 3:
由于编辑 3 开始转向与此处具体解决的问题不同的问题,因此我提出了一个新问题 here。
【问题讨论】:
-
请edit the question 将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。
-
谢谢,我希望它更简洁
-
谢谢,我同意还有其他类似的问题,我在 OP 中标记了其中一个,您可以根据 DebanjanB 的回答从我上面的编辑中看到,似乎没有类似的问题/建议为我的问题工作,我不知道下一步该怎么做,只是要求社区显示我的具体问题(不是你建议的那个特定链接,因为它与 geckodriver/firefox 一起工作,而我的是 chromedriver/chrome,虽然我同意我最终找到的解决方案可能适用于不同的驱动程序)。
标签: selenium google-chrome selenium-webdriver webdriver selenium-chromedriver