我有一个Selenium测试套件,它运行许多测试,在每次新的测试中,它都会在我打开的任何其他窗口之上打开一个浏览器窗口。在当地工作很不和谐。有没有办法告诉硒或OS (Mac)在后台打开窗口?


当前回答

利用它……

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

其他回答

我在Windows中使用了这段代码,得到了答案(参考这里):

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

Options = Options()
Options.headless = True

Driver = webdriver.Firefox(options=Options, executable_path='geckodriver.exe')
Driver.get(...)
...

但我没有在其他浏览器上测试它。

自Chrome 57以来,你有无头的论点:

var options = new ChromeOptions();
options.AddArguments("headless");
using (IWebDriver driver = new ChromeDriver(options))
{
    // The rest of your tests
}

Chrome无头模式比UI版本性能提高30.97%。另一个无头驱动程序PhantomJS比Chrome的无头模式好34.92%。

幻影JSDriver

using (IWebDriver driver = new PhantomJSDriver())
{
     // The rest of your test
}

Firefox无头模式的性能比UI版本提高了3.68%。这很令人失望,因为Chrome的无头模式比UI模式的时间长了30%。另一个无头驱动程序PhantomJS比Chrome的无头模式好34.92%。令我惊讶的是,Edge浏览器击败了所有这些浏览器。

var options = new FirefoxOptions();
options.AddArguments("--headless");
{
    // The rest of your test
}

这可以从Firefox 57+中获得

Firefox无头模式的性能比UI版本提高了3.68%。这很令人失望,因为Chrome的无头模式比UI模式的时间长了30%。另一个无头驱动程序PhantomJS比Chrome的无头模式好34.92%。令我惊讶的是,Edge浏览器击败了所有这些浏览器。

注意:PhantomJS不再维护了!

在*nix上,你也可以运行一个无头的X Window服务器,比如Xvfb,并将DISPLAY环境变量指向它:

用于测试的假X服务器?

如果你使用的是Ubuntu (Gnome),一个简单的解决方法是安装Gnome扩展auto-move-window: https://extensions.gnome.org/extension/16/auto-move-windows/

然后设置浏览器(如。Chrome)到另一个工作空间(例如。2).浏览器将在其他工作空间中静默运行,不再打扰您。你仍然可以在你的工作空间中使用Chrome浏览器,不受任何干扰。

它可能在选项中。下面是相同的Java代码。

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setHeadless(true);
        WebDriver driver = new ChromeDriver(chromeOptions);