我有一个运行很多测试的硒测试套件,在每个新测试中,它都打开了我打开的任何其他窗口顶部的浏览器窗口。在当地环境中工作时非常刺耳。任何方式告诉硒或操作系统(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. 浏览器后面的打开窗口
- 2. 打开窗口浏览器
- 3. Selenium Webdriver可以同时打开不同的浏览器并行执行吗?
- 4. 安装后C#Windows窗体不打开默认浏览器
- 5. 浏览器窗口可能已关闭。 (Selenium :: WebDriver :: Error :: UnknownError)
- 6. 获取当前打开的浏览器窗口在硒2(WebDriver)
- 7. Selenium无法在Linux上打开虚拟浏览器窗口
- 8. web浏览器的I帧在默认浏览器中打开
- 9. 如何打开默认浏览器
- 10. 默认打开IE浏览器
- 11. 打开默认Web浏览器
- 12. Webview loadurl打开默认浏览器
- 13. C++ OS X打开默认浏览器
- 14. 超时::后新的浏览器窗口打开的错误
- 15. 使用Selenium WebDriver在两个浏览器窗口之间切换
- 16. Selenium如何关闭Chrome浏览器并打开新窗口
- 17. 打开Web浏览器点击默认浏览器
- 18. 使用默认浏览器打开浏览器/ URL
- 19. 尽管默认浏览器打开另一个浏览器
- 20. JavaScript:使用浏览器打开窗口
- 21. 用Ruby打开浏览器窗口?
- 22. 如何使用selenium webDriver最小化IE浏览器的窗口?
- 23. 如何在默认为Chrome时在Python中打开一个新的默认浏览器窗口
- 24. 打开新窗口弹出窗口与浏览器分开
- 25. 在后台打开浏览器 - 在后台加载网页
- 26. 从swt浏览器打开外部浏览器窗口
- 27. 无法切换到主窗口内的浏览器窗口(Selenium Webdriver)
- 28. 打开的文件已经在窗口浏览器中打开
- 29. 如何将手动打开的浏览器设置为Selenium webDriver
- 30. 我可以使用Selenium 2来获取HTML数据而无需打开新的浏览器窗口吗?
如果你正在做的'驱动程序= webdriver。Firefox() '在你的代码中,按照我的答案在这里:http://stackoverflow.com/a/23898148/1515819 – 2015-02-01 18:55:04