繁体   English   中英

使用Selenium Java(Mac OSX)将Firefox浏览器置于最前面

[英]Bring the Firefox Browser to Front using selenium Java (Mac OSX)

我正在使用三个firefox驱动程序实例进行自动化。我需要将当前活动的firefox浏览器置于最前面,因为我正在使用一些robo类来进行某些操作。 我曾在Mac中尝试过Google chrome的Java脚本警报(相同操作),并且工作正常。 在Windows中使用user32 lib。 对于firefox mac,它在后台显示警报,但网页不在最前面。

((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();

我在Mac中用于chrome的上述代码。 相同的代码正在工作,并显示有关Firefox的警报,但窗口未显示在最前面。

请建议在firefox中是否还有其他方法可以这样做。

首先将窗口句柄存储在变量中,然后再使用它返回窗口。

//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();

//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')"); 
this.webDriver.switchTo().alert().accept();

//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);

此外,您可以尝试在切换到窗口后最大化它,这也应该激活它。

this.webDriver.manage().window().maximize();

尝试使用窗口名称进行切换:

driver.switchTo().window("windowName");

另外,您可以将“窗口句柄”传递给switchTo().window()方法。 知道这一点,就可以像这样遍历每个打开的窗口:

for (String handle : driver.getWindowHandles()) {
  driver.switchTo().window(handle);
}

基于Selenium文档: http : //docs.seleniumhq.org/docs/03_webdriver.jsp

如其他主题所述,您可以使用

 driver.manage().window().setPosition(new Point(-2000, 0));

太。

# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd()  # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")

在Mac上唯一对我self.driver.fullscreen_window()东西: self.driver.fullscreen_window()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM