我有一个Selenium测试套件,它运行许多测试,在每次新的测试中,它都会在我打开的任何其他窗口之上打开一个浏览器窗口。在当地工作很不和谐。有没有办法告诉硒或OS (Mac)在后台打开窗口?
当前回答
我的chromedriver使用Python和options.add_argument(“headless”)也有同样的问题,但后来我意识到如何修复它,所以我把它放在下面的代码中:
opt = webdriver.ChromeOptions()
opt.arguments.append("headless")
其他回答
我在Windows中使用了这段代码,得到了答案(参考这里):
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
Options = Options()
Options.headless = True
Driver = webdriver.Firefox(options=Options, executable_path='geckodriver.exe')
Driver.get(...)
...
但我没有在其他浏览器上测试它。
有几种方法,但不是简单的“设置配置值”。除非你投资了一个无头浏览器,这并不适合每个人的需求,它是一个有点hack:
如何隐藏Firefox窗口(硒WebDriver)?
and
是否有可能隐藏在硒RC浏览器?
你可以'supposed ',传入一些参数到Chrome,特别是:——no-startup-window
请注意,对于某些浏览器,特别是Internet Explorer,不让它集中运行会影响您的测试。
你也可以使用AutoIt来隐藏窗口。
下面是一个适合我的。net解决方案:
在http://phantomjs.org/download.html下载PhantomJS。
从下载文件夹中的bin文件夹中复制.exe文件,并将其粘贴到Visual Studio项目的bin调试/发布文件夹中。
使用
using OpenQA.Selenium.PhantomJS;
在你的代码中,像这样打开驱动程序:
PhantomJSDriver driver = new PhantomJSDriver();
using (driver)
{
driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/login");
// Your code here
}
这是一个简单的Node.js解决方案,适用于新版本4。x(也可能是3.x)的硒。
铬
const { Builder } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');
let driver = await new Builder().forBrowser('chrome').setChromeOptions(new chrome.Options().headless()).build()
await driver.get('https://example.com')
火狐
const { Builder } = require('selenium-webdriver')
const firefox = require('selenium-webdriver/firefox');
let driver = await new Builder().forBrowser('firefox').setFirefoxOptions(new firefox.Options().headless()).build()
await driver.get('https://example.com')
整个程序都在后台运行。这正是我们想要的。
在Windows上,你可以使用win32gui:
import win32gui
import win32con
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, win32con.SW_MINIMIZE)
win32gui.ShowWindow(self.hwnd, win32con.SW_HIDE)
def show(self):
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)
win32gui.ShowWindow(self.hwnd, win32con.SW_MAXIMIZE)
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, e.q., phantomjs.exe, notepad.exe, etc.
# To hide the window
hider.hide()
# To show again
hider.show()
然而,这个解决方案有一个问题-使用send_keys方法使窗口显示出来。你可以使用JavaScript来处理它,不显示窗口:
def send_keys_without_opening_window(id_of_the_element, keys)
YourWebdriver.execute_script("document.getElementById('" + id_of_the_element + "').value = '" + keys + "';")
推荐文章
- Selenium c# WebDriver:等待元素出现
- Selenium WebDriver可以在后台无声地打开浏览器窗口吗?
- 我如何在python中使用selenium webdriver滚动网页?
- 如何验证一个XPath表达式在Chrome开发工具或Firefox的Firebug?
- 我如何获得当前的URL在硒Webdriver 2 Python?
- 滚动元素到视图与硒
- 在MSTest中[TearDown]和[SetUp]的替代方案是什么?
- MacOS Catalina(v 10.15.3):错误:“chromedriver”不能打开,因为开发人员无法验证。无法启动chrome浏览器
- 在Selenium中等待页面加载
- 如何选择一个下拉菜单值与硒使用Python?
- 等待页面加载Selenium WebDriver for Python
- WebDriverException:未知错误:DevToolsActivePort文件不存在,而试图启动Chrome浏览器
- 在Selenium中键入回车键
- webdriver.Dispose(), .Close()和.Quit()的区别
- 错误信息:"'chromedriver'可执行文件需要在路径中可用"