Sté*_*ert 64
隐藏浏览器的最简单方法是安装PhantomJS.然后,更改此行:
driver = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)
至:
driver = webdriver.PhantomJS()
Run Code Online (Sandbox Code Playgroud)
其余代码不需要更改,也不会打开任何浏览器.出于调试目的,请driver.save_screenshot('screen.png')
在代码的不同步骤中使用,或者再次切换到Firefox webdriver.
在Windows上,您必须指定phantomjs.exe的路径:
driver = webdriver.PhantomJS('C:\phantomjs-1.9.7-windows\phantomjs.exe')
Run Code Online (Sandbox Code Playgroud)
看看Ghost Driver:如何使用java运行Selenium的ghostdriver
C#
如何隐藏FirefoxDriver(使用Selenium)或将其放在C#Form中
Ove*_*TAR 16
最后,我找到了那些使用windows Machine运行测试的人使用任何方法的解决方案.好吧,实现不是Java,但你可以很容易地实现.
使用AutoIt
工具.它具有处理窗口的所有功能.这是一个免费的工具.
安装AutoIt:http://www.autoitscript.com/site/autoit/downloads/
打开编辑器,写下隐藏任何窗口的代码.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", @SW_HIDE)
Run Code Online (Sandbox Code Playgroud)要取消隐藏它,您可以使用以下代码行.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", @SW_SHOW)
Run Code Online (Sandbox Code Playgroud)
WinTitleMatchMode
有不同的选项,可用于匹配Windows标题.
1 = Match the title from the start (default)`
2 = Match any substring in the title
3 = Exact title match
4 = Advanced mode, see Window Titles & Text (Advanced)
Run Code Online (Sandbox Code Playgroud)所以,我所做的是:我创建了一个小程序的.exe文件,并将参数作为命令行参数传递,如下所示.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
Run Code Online (Sandbox Code Playgroud)
in HideNSeek.exe
- 我有以下AutoIt代码:
AutoItSetOption("WinTitleMatchMode", 1)
if $CmdLine[0] > 0 Then
if $CmdLine[1] == 0 Then
WinSetState($CmdLine[2], "", @SW_HIDE)
ElseIf $CmdLine[1] == 1 Then
WinSetState($CmdLine[2], "", @SW_SHOW)
Else
EndIf
EndIf
Run Code Online (Sandbox Code Playgroud)
$CmdLine[]
是一个数组,它将包含所有命令行参数...
$CmdLine[0] = number of Parameter
$CmdLine[1] = 1st Parameter after Exe Name
...
Run Code Online (Sandbox Code Playgroud)
如果窗口标题中有任何空格,则必须使用双引号将其作为命令行参数传递,如上所述.
下面的代码行将执行AutoIt exe,如果我在第一个参数中传递'0',那么它将隐藏窗口,如果我将传递'1',它将取消隐藏与标题匹配的窗口.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
Run Code Online (Sandbox Code Playgroud)
我希望这能帮到您.谢谢!
Joh*_*hny 12
只是做(Python):
opts = webdriver.FirefoxOptions()
opts.headless = True
firefox = webdriver.Firefox(options=opts)
Run Code Online (Sandbox Code Playgroud)
Nic*_*nko 11
我使用xvfb来解决这个问题.
首先,安装Xvfb:
# apt-get install xvfb
Run Code Online (Sandbox Code Playgroud)
在Debian/Ubuntu上; 要么
# yum install xorg-x11-Xvfb
Run Code Online (Sandbox Code Playgroud)
在Fedora/RedHat上.然后,选择一个不太可能发生冲突的显示数字(即使你以后添加一个真实的显示) - 像99这样的高点应该做的事情.在此屏幕上运行Xvfb,关闭访问控制:
# Xvfb :99 -ac
Run Code Online (Sandbox Code Playgroud)
现在,您需要确保在运行Selenium服务器(它本身启动浏览器)之前将显示设置为99.最简单的方法是将DISPLAY =:99导出到Selenium的环境中.首先,确保事情正在从命令行运行,如下所示:
$ export DISPLAY=:99
$ firefox
Run Code Online (Sandbox Code Playgroud)
要不就
$ DISPLAY=:99 firefox
Run Code Online (Sandbox Code Playgroud)
下面有一个帮助我的链接
http://www.alittlemadness.com/2008/03/05/running-selenium-headless/
小智 10
只需添加以下代码,即可正常工作。
import os
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)
小智 8
只需添加这些,如果您使用的是 chrome,它就可以工作,在 Firefox 中也很有用
from selenium.webdriver.chrome.options import Options
'''option to make driver work background'''
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
Run Code Online (Sandbox Code Playgroud)
PhantomJS的默认浏览器是IE,尽管许多浏览器功能在这里都无法使用。如果要打开无头(隐藏)的Firefox窗口,则可以使用Firefox 56+的新功能。
使用此功能,您可以得到如下headless
驱动程序:
System.setProperty("webdriver.gecko.driver", firefoxDriverExePath);
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
FirefoxDriver driver = new FirefoxDriver(options);
Run Code Online (Sandbox Code Playgroud)
新版本的Chrome也可以headless
选择。
归档时间: |
|
查看次数: |
76456 次 |
最近记录: |