2013-04-23 154 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

相關問題