使用 selenium Java (Mac OSX) 将 Firefox 浏览器置于前端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:41:38  来源:igfitidea点击:

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

javafirefoxselenium-webdriver

提问by Jojo John

I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.

我正在使用三个 firefox 驱动程序实例进行自动化。我需要将当前活动的 firefox 浏览器放在前面,因为我正在使用一些 robo 类进行一些操作。我曾在 mac 中为谷歌浏览器尝试过 java 脚本警报(相同的操作)并且它工作正常。在 Windows 中使用 user32 lib。在 firefox mac 的情况下,它在后台显示警报,但网页没有出现在前面。

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

The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.

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

Please suggest if there any other method for doing the same in firefox.

请建议是否有任何其他方法可以在 Firefox 中执行相同操作。

采纳答案by Faiz

Store the window handle first in a variable, and then use it to go back to the window later on.

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

//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);

Additionally, you can try to maximise the window after switching to it, which should also activate it.

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

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

回答by RAlex

Try switching using the window name:

尝试使用窗口名称切换:

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

Alternatively, you can pass a "window handle" to the switchTo().window()method. Knowing this, it's possible to iterate over every open window like so:

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

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

Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp

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

回答by Jaroslav ?treit

As described in other topics, you can use

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

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

too.

也。

回答by VjyAnnd

# 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")

回答by Fusion

Only thing that worked for me on mac: self.driver.fullscreen_window().

唯一在 mac 上对我有用的东西:self.driver.fullscreen_window().