我有一个Selenium测试套件,它运行许多测试,在每次新的测试中,它都会在我打开的任何其他窗口之上打开一个浏览器窗口。在当地工作很不和谐。有没有办法告诉硒或OS (Mac)在后台打开窗口?
当前回答
在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 + "';")
其他回答
这是一个简单的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')
整个程序都在后台运行。这正是我们想要的。
有几种方法,但不是简单的“设置配置值”。除非你投资了一个无头浏览器,这并不适合每个人的需求,它是一个有点hack:
如何隐藏Firefox窗口(硒WebDriver)?
and
是否有可能隐藏在硒RC浏览器?
你可以'supposed ',传入一些参数到Chrome,特别是:——no-startup-window
请注意,对于某些浏览器,特别是Internet Explorer,不让它集中运行会影响您的测试。
你也可以使用AutoIt来隐藏窗口。
我的chromedriver使用Python和options.add_argument(“headless”)也有同样的问题,但后来我意识到如何修复它,所以我把它放在下面的代码中:
opt = webdriver.ChromeOptions()
opt.arguments.append("headless")
它可能在选项中。下面是相同的Java代码。
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setHeadless(true);
WebDriver driver = new ChromeDriver(chromeOptions);
实现这一点的一种方法是在headless模式下运行浏览器。这样做的另一个好处是测试执行得更快。
请找到下面的代码在Chrome浏览器中设置无头模式。
package chrome;
public class HeadlessTesting {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
"ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.get("https://contentstack.built.io");
driver.get("https://www.google.co.in/");
System.out.println("title is: " + driver.getTitle());
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));
driver.quit();
}
}
推荐文章
- 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'可执行文件需要在路径中可用"
- 我如何在Selenium WebDriver (Python)中找到包含特定文本的元素?