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


当前回答

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

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

其他回答

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

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

如果你在Python中使用Selenium web驱动程序,你可以使用PyVirtualDisplay,它是Xvfb和Xephyr的Python包装器。

PyVirtualDisplay需要Xvfb作为依赖项。在Ubuntu上,首先安装Xvfb:

sudo apt-get install xvfb

然后从PyPI安装PyVirtualDisplay:

pip install pyvirtualdisplay

使用PyVirtualDisplay在无头模式下的Python Selenium脚本示例:

    #!/usr/bin/env python

    from pyvirtualdisplay import Display
    from selenium import webdriver

    display = Display(visible=0, size=(800, 600))
    display.start()

    # Now Firefox will run in a virtual display.
    # You will not see the browser.
    browser = webdriver.Firefox()
    browser.get('http://www.google.com')
    print browser.title
    browser.quit()

    display.stop()

EDIT

最初的答案是在2014年发布的,现在我们正处于2018年的风口浪尖。和其他事物一样,浏览器也在进步。Chrome现在有一个完全无头的版本,它消除了使用任何第三方库来隐藏UI窗口的需要。示例代码如下:

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

    CHROME_PATH = '/usr/bin/google-chrome'
    CHROMEDRIVER_PATH = '/usr/bin/chromedriver'
    WINDOW_SIZE = "1920,1080"

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
    chrome_options.binary_location = CHROME_PATH

    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,
                              chrome_options=chrome_options
                             )
    driver.get("https://www.google.com")
    driver.get_screenshot_as_file("capture.png")
    driver.close()

实现这一点的一种方法是在headless模式下运行浏览器。这样做的另一个好处是测试执行得更快。

请找到下面的代码在Chrome浏览器中设置无头模式。

package chrome;

public class HeadlessTesting {

    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.chrome.driver",
                "ChromeDriverPath");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("window-size=1200x600");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://contentstack.built.io");
        driver.get("https://www.google.co.in/");
        System.out.println("title is: " + driver.getTitle());
        File scrFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));
        driver.quit();
    }
}

下面是一个适合我的。net解决方案:

在http://phantomjs.org/download.html下载PhantomJS。

从下载文件夹中的bin文件夹中复制.exe文件,并将其粘贴到Visual Studio项目的bin调试/发布文件夹中。

使用

using OpenQA.Selenium.PhantomJS;

在你的代码中,像这样打开驱动程序:

PhantomJSDriver driver = new PhantomJSDriver();
using (driver)
{
   driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/login");
   // Your code here
}

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

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