将 Selenium 与 Chromium 浏览器结合使用
在 Selenium 选项(在 Firefox 上)中,我可以找到自定义浏览器
。
是否可以使用此选项在 Chromium 浏览器(不是 Chrome)中运行 Selenium 测试?
In the Selenium options (on Firefox) I can find Custom browser
.
Is it possible to use this option to run a Selenium test in Chromium Browser (not Chrome)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
呃,接受的答案没有回答问题。 Google Chrome 基于 Chromium,但它们不是同一个浏览器。这就是您想要的:(因为 Chromium 不受官方支持)
编辑 2018-08:看起来接受的答案已更改为副本几年后,我原来的评论不再正确。我把它留在那里,但删除了,因为如果我直接删除它,投票就会产生误导。
Uh, the accepted answer doesn't answer the question. Google Chrome is based on Chromium, but they're not the same browser.This is what you want: (since Chromium isn't officially supported)
Edit 2018-08: Looks like the accepted answer changed to a copy of this one several years later, so my original comment is no longer correct. I'm leaving it there, but struck out, because the votes are misleading if I straight remove it.
在 Unix 系统上,您可以执行类似的操作
,然后在创建 DefaultSelenium 实例时可以使用“*googlechrome”作为启动参数。
On Unix systems, you can do something like
And then you can use "*googlechrome" as the launch parameter when creating your DefaultSelenium instance.
(Python)
您可以使用 chromium-chromedriver 而不是普通的 chromedriver。它可以通过 apt-get 安装,例如“sudo apt-get install chromium-chromedriver”
然后在我的脚本中,我配置 chromebrowser 和驱动程序以使用 chromium exe 和 chromedriver exe,例如:
(Python)
You can use chromium-chromedriver instead of the vanilla chromedriver. It can be installed via apt-get like "sudo apt-get install chromium-chromedriver"
In my scripts I then configure the chromebrowser and driver to use the chromium exe and chromedriver exe like:
是的。对于 Chromium,使用:
您可以使用的其他选项是 *custom、*chrome(注意:这不是 Google Chrome;它只是 Firefox 模式)、*googlechrome、*iexplore。请检查 Selenium 文档以获取模式的完整列表。
Yes. For Chromium, use:
The other options that you can use are *custom, *chrome (note: this is not Google Chrome; it’s a Firefox mode only), *googlechrome, *iexplore. Please check the Selenium documentation for complete list of the modes.
这可能太简单了,我会弄清楚我所做的事情是非常错误的,但是......
似乎有效......
It's probably too easy, and I'm going to figure out what I did that is horribly wrong, but...
appears to work...
对我来说,只需添加:
chrome_options.binary_location = "/usr/bin/chromium-browser"
示例代码:
它适用于
版本 104.0.5112.101(官方版本)构建于 Ubuntu,运行于Ubuntu 18.04(64 位)
。For me, just add:
chrome_options.binary_location = "/usr/bin/chromium-browser"
Sample code:
It works with
Version 104.0.5112.101 (Official Build) Built on Ubuntu, running on Ubuntu 18.04 (64-bit)
.是的,它是...
在 Linux 中,您可以安装并使用,无需 X Window (例如:在网络服务器中)也是......这对于一些测试来说很好。
在代码中,您需要一个 chromedriver。看看这个:
chromedriver
在本例中,我将使用 Python 代码来打开 Chromium 实例在无头模式下:
观察:
就是这样!
Yes, it is...
In a Linux you can install, to use without an X Window (example: in a webserver) too... It’s nice to some tests.
In code, you'll need a chromedriver. Look at this:
chromedriver
In this case I'll use Python code, to open a Chromium instance in a headless mode:
Obs.:
That's it!
在 Ubuntu 上,我通过像标准 Chrome 一样从 Selenium 启动 Chromium 来运行 Chromium,无需通过安装 chromium-chromedriver 包进行任何修改。
TL;DR:
如果您碰巧运行 Ubuntu,它切换到了那个有问题的 Chromium snap,那么已经包含了一个合适的 chromedriver。可执行文件已位于
PATH
上:/snap/bin/chromium.chromedriver
。如果您也碰巧使用在
PATH
中搜索chromedriver
的普通 selenium,那么所谓的过渡性 do-nothing-packagechromium-chromedriver
(dpkg) 实际上提供了:一个“可执行”(shell 脚本)/usr/bin/chromedriver
执行上面提到的 snap 版本。在这个 Ubuntu 20.04 (Focal Fossa) 安装中,我也发生了拥有这个过渡性的 chromium-browser 包,这可能根本不需要,但它也提供了(没用?)包装器,让我首先尝试匹配的 chromium-chromedriver 包版本。我在焦点更新的
1:85.0.4183.83-0ubuntu0.20.04.2
上都有。在 REPL 中:
On Ubuntu, I managed to run Chromium by launching it from Selenium like a standard Chrome without any modifications by installing the
chromium-chromedriver
package.TL;DR:
If you happen to run Ubuntu, which switched to that questionable Chromium snap, a fitting chromedriver is already included. The executable is on the the
PATH
already:/snap/bin/chromium.chromedriver
.And if you also happen to use a vanilla selenium which searches for
chromedriver
in thePATH
, the supposedly transitional do-nothing-packagechromium-chromedriver
(dpkg) is actually providing that: an “executable” (shell-script)/usr/bin/chromedriver
exec-ing the above mentioned snap version.On this Ubuntu 20.04 (Focal Fossa) installation, I also happened to have this transitional
chromium-browser
package, which is probably not needed at all, but it also provides a (useless?) wrapper and made me try a matchingchromium-chromedriver
package version first. I have both on1:85.0.4183.83-0ubuntu0.20.04.2
from focal-updates.In the REPL:
这里有点晚了:-)但是我的贡献答案这里可能对任何有兴趣使用 chromium 和基于 arm64 的 Selenium 镜像的人有用。
A bit late here :-) but my contributing answer here might be useful for anyone interested in using chromium and arm64-based Selenium images.