我有一個運行很多測試的硒測試套件,在每個新測試中,它都打開了我打開的任何其他窗口頂部的瀏覽器窗口。在當地環境中工作時非常刺耳。任何方式告訴硒或操作系統(MAC)在後臺打開窗戶?Selenium Webdriver可以在後臺默默打開瀏覽器窗口嗎?
回答
有幾種方法,但它不是一個簡單的「設置配置值」。除非你在一個無頭的瀏覽器,它不適合每個人的需求的投資,這是一個黑客的一點點:
How to hide Firefox window (Selenium WebDriver)?
和
Is it possible to hide the browser in Selenium RC?
你可以用「據說」,將某些參數傳入Chrome,具體如下:--no-startup-window
請注意,對於某些瀏覽器,尤其是IE瀏覽器,它會傷害您的測試,導致它無法在焦點上運行。
你也可以用AutoIT破解一下,一旦它打開就隱藏窗口。
「--no-startup-window」現已棄用 – 2017-11-10 04:16:36
在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+"';")
在* nix,你也可以運行一個無頭的X服務器類似的Xvfb,並指出了DISPLAY變量是:
您可以通過使用javascript不顯示窗口處理它如果您在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()
一個美觀,乾淨的解決方案。感謝那。奇蹟般有效。當之無愧+ 1 – Eldamir 2014-05-27 07:42:43
適用於Mac OSX嗎? – vanguard69 2015-07-19 12:44:11
如果您使用的是Ubuntu並且您的測試套件是Python,那麼這非常棒 – kevzettler 2016-02-10 23:08:42
我建議用幻影的js 獲取更多信息,您需要訪問Phantom Official Website
據我所知PhantomJS只用Firefox工作..
下載PhantomJs.exe後您需要導入到您的項目中,如下圖所示Phantomjs內部爲常見 >>天秤座RY >>phantomjs.exe
現在你有你的硒代碼裏面是改變線路
browser = webdriver.Firefox()
要像
import os
path2phantom = os.getcwd() + "\common\Library\phantomjs.exe"
browser = webdriver.PhantomJS(path2phantom)
路徑到phantomjs可能會不同...如你所願改變:)
就是這樣,它爲我工作。肯定他會爲你工作,乾杯
雖然這個鏈接可能回答這個問題,最好包括的基本部分在這裏回答並提供參考鏈接。如果鏈接頁面發生變化,僅鏈接答案可能會失效 – slfan 2016-05-29 15:39:46
這裏是一個.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
}
的Chrome 57有一個選項傳遞--headless標誌,這使得窗口不可見。
該標誌與--no-startup-window不同,因爲最後一個不啓動窗口。它用於託管後臺應用程序,如this page所述。
Java代碼的標誌傳遞給硒的webdriver(ChromeDriver):
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
ChromeDriver chromeDriver = new ChromeDriver(options);
對於運行沒有任何瀏覽器,可以在無頭模式下運行它。
我告訴你在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
- 1. Selenium webdriver可以附加到已經打開的瀏覽器窗口嗎?
- 2. Selenium/PHPUnit。是否可以在窗口/瀏覽器之上打開一個乾淨的新窗口/瀏覽器?
- 3. 打開窗口瀏覽器
- 4. 瀏覽器後面的打開窗口
- 5. Coffeescript在後臺打開新的瀏覽器窗口
- 6. Selenium Webdriver可以同時打開不同的瀏覽器並行執行嗎?
- 7. 如何使用Selenium WebDriver for python在瀏覽器上打開一個新窗口?
- 8. 安裝後C#Windows窗體不打開默認瀏覽器
- 9. 在默認瀏覽器中打開URL
- 10. 瀏覽器窗口可能已關閉。 (Selenium :: WebDriver :: Error :: UnknownError)
- 11. 獲取當前打開的瀏覽器窗口在硒2(WebDriver)
- 12. 帶Webdriver取樣器的JMeter - 瀏覽器窗口未打開
- 13. Selenium無法在Linux上打開虛擬瀏覽器窗口
- 14. web瀏覽器的I幀在默認瀏覽器中打開
- 15. Webview loadurl打開默認瀏覽器
- 16. 默認打開IE瀏覽器
- 17. 如何打開默認瀏覽器
- 18. 瀏覽器:打開默認主頁
- 19. 打開默認Web瀏覽器
- 20. C++ OS X打開默認瀏覽器
- 21. 超時::後新的瀏覽器窗口打開的錯誤
- 22. 我可以使用selenium webdriver讀取瀏覽器網址嗎?
- 23. 使用Selenium WebDriver在兩個瀏覽器窗口之間切換
- 24. 使用Selenium Webdriver在瀏覽器窗口之間切換
- 25. 在後臺打開窗口
- 26. Selenium如何關閉Chrome瀏覽器並打開新窗口
- 27. 打開窗口,瀏覽它
- 28. 打開Web瀏覽器點擊默認瀏覽器
- 29. 使用默認瀏覽器打開瀏覽器/ URL
- 30. 儘管默認瀏覽器打開另一個瀏覽器
如果你正在做的'驅動程序= webdriver。Firefox() '在你的代碼中,按照我的答案在這裏:http://stackoverflow.com/a/23898148/1515819 – 2015-02-01 18:55:04