2013-04-23 242 views
92

我有一个运行很多测试的硒测试套件,在每个新测试中,它都打开了我打开的任何其他窗口顶部的浏览器窗口。在当地环境中工作时非常刺耳。任何方式告诉硒或操作系统(MAC)在后台打开窗户?Selenium Webdriver可以在后台默默打开浏览器窗口吗?

+0

如果你正在做的'驱动程序= webdriver。Firefox() '在你的代码中,按照我的答案在这里:http://stackoverflow.com/a/23898148/1515819 – 2015-02-01 18:55:04

回答

47

有几种方法,但它不是一个简单的“设置配置值”。除非你在一个无头的浏览器,它不适合每个人的需求的投资,这是一个黑客的一点点:

How to hide Firefox window (Selenium WebDriver)?

Is it possible to hide the browser in Selenium RC?

你可以用“据说”,将某些参数传入Chrome,具体如下:--no-startup-window

请注意,对于某些浏览器,尤其是IE浏览器,它会伤害您的测试,导致它无法在焦点上运行。

你也可以用AutoIT破解一下,一旦它打开就隐藏窗口。

+0

“--no-startup-window”现已弃用 – 2017-11-10 04:16:36

4

在Windows中可以使用win32gui:

import win32gui 
import subprocess 

class HideFox: 
    def __init__(self, exe='firefox.exe'): 
     self.exe = exe 
     self.get_hwnd() 


    def get_hwnd(self): 
     win_name = get_win_name(self.exe) 
     self.hwnd = win32gui.FindWindow(0,win_name) 


    def hide(self): 
     win32gui.ShowWindow(self.hwnd, 6) 
     win32gui.ShowWindow(self.hwnd, 0) 

    def show(self): 
     win32gui.ShowWindow(self.hwnd, 5) 
     win32gui.ShowWindow(self.hwnd, 3) 

def get_win_name(exe): 
    '''simple function that gets the window name of the process with the given name''' 
    info = subprocess.STARTUPINFO() 
    info.dwFlags |= subprocess.STARTF_USESHOWWINDOW 
    raw=subprocess.check_output('tasklist /v /fo csv', startupinfo=info).split('\n')[1:-1] 
    for proc in raw: 
     try: 
      proc=eval('['+proc+']') 
      if proc[0]==exe: 
       return proc[8]    
     except: 
      pass 
    raise ValueError('Could not find a process with name '+exe) 

例子:

hider=HideFox('firefox.exe') #can be anything, eq: phantomjs.exe, notepad.exe ... 
#To hide the window 
hider.hide() 
#To show again 
hider.show() 

但是有一个问题与此解决方案 - 使用send_keys方法使窗口显示出来。

def send_keys_without_opening_window(id_of_the_element, keys) 
    YourWebdriver.execute_script("document.getElementById('" +id_of_the_element+"').value = '"+keys+"';") 
0

在* nix,你也可以运行一个无头的X服务器类似的Xvfb,并指出了DISPLAY变量是:

Fake X server for testing?

您可以通过使用javascript不显示窗口处理它
97

如果您在Python中使用Selenium Web驱动程序,则可以使用PyVirtualDisplay,一种用于Xvfb和Xephyr的Python包装程序。

PyVirtualDisplay需要Xvfb作为依赖项。在Ubuntu上,首先安装的Xvfb:在无头模式PyVirtualDisplay在Python

pip install pyvirtualdisplay 

样品硒脚本:

sudo apt-get install xvfb 

然后从PyPI将安装PyVirtualDisplay

#!/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() 

编辑 最初的答案是在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() 
+0

一个美观,干净的解决方案。感谢那。奇迹般有效。当之无愧+ 1 – Eldamir 2014-05-27 07:42:43

+7

适用于Mac OSX吗? – vanguard69 2015-07-19 12:44:11

+0

如果您使用的是Ubuntu并且您的测试套件是Python,那么这非常棒 – kevzettler 2016-02-10 23:08:42

7

我建议用幻影的js 获取更多信息,您需要访问Phantom Official Website

据我所知PhantomJS只用Firefox工作..

下载PhantomJs.exe后您需要导入到您的项目中,如下图所示Phantomjs内部为常见 >>天秤座RY >>phantomjs.exe enter image description here

现在你有你的硒代码里面是改变线路

browser = webdriver.Firefox() 

要像

import os 
path2phantom = os.getcwd() + "\common\Library\phantomjs.exe" 
browser = webdriver.PhantomJS(path2phantom) 

路径到phantomjs可能会不同...如你所愿改变:)

就是这样,它为我工作。肯定他会为你工作,干杯

+0

虽然这个链接可能回答这个问题,最好包括的基本部分在这里回答并提供参考链接。如果链接页面发生变化,仅链接答案可能会失效 – slfan 2016-05-29 15:39:46

0

这里是一个.NET解决方案为我工作:

此处下载PhantomJs http://phantomjs.org/download.html

拷贝bin文件夹中下载和粘贴该.exe在Visual Studio项目的bin debug/release文件夹中。

添加此使用

using OpenQA.Selenium.PhantomJS; 

在代码中打开驱动程序是这样的:

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

的Chrome 57有一个选项传递--headless标志,这使得窗口不可见。

该标志与--no-startup-window不同,因为最后一个不启动窗口。它用于托管后台应用程序,如this page所述。

Java代码的标志传递给硒的webdriver(ChromeDriver):

ChromeOptions options = new ChromeOptions(); 
options.addArguments("--headless"); 
ChromeDriver chromeDriver = new ChromeDriver(options); 
+0

您知道VBA语言中是否可以这样做? – Martin 2017-08-01 08:52:39

+0

@Martin我不知道你的问题是否已经修复,但经过快速搜索,我找到了那些:https://github.com/prajaktamoghe/selenium-vba/issues/33 https:// stackoverflow .com/questions/45121955/how-can-i-use-chrome-options-in-vba-with-selenium 这对你有帮助吗? – in016hoe 2017-08-23 14:36:03

0

对于运行没有任何浏览器,可以在无头模式下运行它。

我告诉你在Python一个例子是为我工作,现在

from selenium import webdriver 


options = webdriver.ChromeOptions() 
options.add_argument("headless") 
self.driver = webdriver.Chrome(executable_path='/Users/${userName}/Drivers/chromedriver', chrome_options=options) 

我还多一点信息的添加您这在谷歌官方网站https://developers.google.com/web/updates/2017/04/headless-chrome

相关问题