将 Selenium 与 Chromium 浏览器结合使用

发布于 2024-11-02 05:57:36 字数 136 浏览 7 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

疑心病 2024-11-09 05:57:36

呃,接受的答案没有回答问题。 Google Chrome 基于 Chromium,但它们不是同一个浏览器。

这就是您想要的:(因为 Chromium 不受官方支持)

DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*custom C:/path/to/chromium.exe" , "www.google.com");
selenium.start();

编辑 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)

DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*custom C:/path/to/chromium.exe" , "www.google.com");
selenium.start();

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.

好倦 2024-11-09 05:57:36

在 Unix 系统上,您可以执行类似的操作

sudo ln -s /usr/lib/chromium-browser/chromium-browser /usr/bin/google-chrome

,然后在创建 DefaultSelenium 实例时可以使用“*googlechrome”作为启动参数。

On Unix systems, you can do something like

sudo ln -s /usr/lib/chromium-browser/chromium-browser /usr/bin/google-chrome

And then you can use "*googlechrome" as the launch parameter when creating your DefaultSelenium instance.

长安忆 2024-11-09 05:57:36

(Python)

您可以使用 chromium-chromedriver 而不是普通的 chromedriver。它可以通过 apt-get 安装,例如“sudo apt-get install chromium-chromedriver”

然后在我的脚本中,我配置 chromebrowser 和驱动程序以使用 chromium exe 和 chromedriver exe,例如:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.BinaryLocation = "/usr/bin/chromium-browser"

driver = webdriver.Chrome(executable_path="/usr/bin/chromedriver",options=options)
driver.get("https://www.google.com")

(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:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.BinaryLocation = "/usr/bin/chromium-browser"

driver = webdriver.Chrome(executable_path="/usr/bin/chromedriver",options=options)
driver.get("https://www.google.com")
相思故 2024-11-09 05:57:36

是的。对于 Chromium,使用:

DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*custom path/to/chromium", "www.google.com");
selenium.start();

您可以使用的其他选项是 *custom、*chrome(注意:这不是 Google Chrome;它只是 Firefox 模式)、*googlechrome、*iexplore。请检查 Selenium 文档以获取模式的完整列表。

Yes. For Chromium, use:

DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*custom path/to/chromium", "www.google.com");
selenium.start();

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.

只有一腔孤勇 2024-11-09 05:57:36

这可能太简单了,我会弄清楚我所做的事情是非常错误的,但是......

    ChromeOptions options = new ChromeOptions();

    options.BinaryLocation = "C:\Program Files (x86)\Chromium\Application\chrome.exe");

    using (var chrome = new ChromeDriver(options))

似乎有效......

It's probably too easy, and I'm going to figure out what I did that is horribly wrong, but...

    ChromeOptions options = new ChromeOptions();

    options.BinaryLocation = "C:\Program Files (x86)\Chromium\Application\chrome.exe");

    using (var chrome = new ChromeDriver(options))

appears to work...

灯下孤影 2024-11-09 05:57:36

对我来说,只需添加:

chrome_options.binary_location = "/usr/bin/chromium-browser"

示例代码:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=1, size=(1600, 902))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--start-maximized")
chrome_options.binary_location = "/usr/bin/chromium-browser"
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.delete_all_cookies()
driver.set_window_size(800, 800)
driver.set_window_position(0, 0)
print("arguments done")
driver.get("http://google.com")

它适用于 版本 104.0.5112.101(官方版本)构建于 Ubuntu,运行于Ubuntu 18.04(64 位)

For me, just add:

chrome_options.binary_location = "/usr/bin/chromium-browser"

Sample code:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=1, size=(1600, 902))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--start-maximized")
chrome_options.binary_location = "/usr/bin/chromium-browser"
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.delete_all_cookies()
driver.set_window_size(800, 800)
driver.set_window_position(0, 0)
print("arguments done")
driver.get("http://google.com")

It works with Version 104.0.5112.101 (Official Build) Built on Ubuntu, running on Ubuntu 18.04 (64-bit).

预谋 2024-11-09 05:57:36

是的,它是...

在 Linux 中,您可以安装并使用,无需 X Window (例如:在网络服务器中)也是......这对于一些测试来说很好。

apt install chromium-shell

在代码中,您需要一个 chromedriver。看看这个:

chromedriver

在本例中,我将使用 Python 代码来打开 Chromium 实例在无头模式下:

def startBot():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome('/opt/chromedriver85', options=chrome_options)
    #driver.set_window_size(1366, 728)
    #aguardar carregamento em segundos
    driver.implicitly_wait(5)

    print("Get URL...")
    driver.get("https://www.google.com")

观察:

无头浏览器是自动化测试和服务器环境的绝佳工具,在这些环境中您不需要可见的 UI shell。 (来源

就是这样!

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.

apt install chromium-shell

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:

def startBot():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome('/opt/chromedriver85', options=chrome_options)
    #driver.set_window_size(1366, 728)
    #aguardar carregamento em segundos
    driver.implicitly_wait(5)

    print("Get URL...")
    driver.get("https://www.google.com")

Obs.:

A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell. (source)

That's it!

傻比既视感 2024-11-09 05:57:36

在 Ubuntu 上,我通过像标准 Chrome 一样从 Selenium 启动 Chromium 来运行 Chromium,无需通过安装 chromium-chromedriver 包进行任何修改。

TL;DR:

如果您碰巧运行 Ubuntu,它切换到了那个有问题的 Chromium snap,那么已经包含了一个合适的 chromedriver。可执行文件已位于 PATH 上:/snap/bin/chromium.chromedriver

如果您也碰巧使用在 PATH 中搜索 chromedriver 的普通 selenium,那么所谓的过渡性 do-nothing-package chromium-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 上都有。

python3 -m venv venv
. venv/bin/activate
pip install selenium
python3

REPL 中:

from selenium import webdriver
d = webdriver.Chrome()

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 the PATH, the supposedly transitional do-nothing-package chromium-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 matching chromium-chromedriver package version first. I have both on 1:85.0.4183.83-0ubuntu0.20.04.2 from focal-updates.

python3 -m venv venv
. venv/bin/activate
pip install selenium
python3

In the REPL:

from selenium import webdriver
d = webdriver.Chrome()
没有心的人 2024-11-09 05:57:36

这里有点晚了:-)但是我的贡献答案这里可能对任何有兴趣使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文