简体   繁体   English

带来浏览器从头到尾? (Selenium Web驱动程序,Java)

[英]Bring browser from back to front? (Selenium Web Driver, Java)

Is there any way I can bring browser from the back to the top(front)?? 有什么办法可以使浏览器从后到顶部(前)?

The situation is that there are two browsers, firefox and chrome. 情况是有两种浏览器,firefox和chrome。 So, I instantiated two drivers, new FirefoxDriver() and new ChromeDriver(), let's call them fdriver and cdriver. 因此,我实例化了两个驱动程序,新的FirefoxDriver()和新的ChromeDriver(),我们将它们称为fdriver和cdriver。

What I want is when the program is using Firefox, the firefox browser should be on the top. 我想要的是当程序使用Firefox时,firefox浏览器应该位于顶部。 And so does Chrome. Chrome也是如此。 But, I am stuck how to bring the browser to the top when they are on the back. 但是,我被困在如何将浏览器置于背面的情况下使其位于顶部。

I already tried, 我已经尝试过了

Javascript: self.focus() and window.focus(). Javascript:self.focus()和window.focus()。 / WebDriverBackedSelenium to make driver back to selenium and use windowMaximize and windowFocus / WebDriverBackedSelenium使驱动程序返回到硒并使用windowMaximize和windowFocus

Any idea is appreciated, thanks 任何想法表示赞赏,谢谢

You should be able to do this with 您应该能够做到这一点

driver.SwitchTo().Window("//name of the window");

and that will bring whatever window you want into focus. 这将使您想要聚焦的任何窗口。

Now I don't use selenium, but I use Geb which is a wrapper around selenium so calling the javascript may be different for you, but this is how I did it (should be similar) 现在我不使用硒,但我使用的是Geb,它是硒的包装材料,因此调用javascript对您来说可能有所不同,但这就是我的做法(应该类似)

browser.js."alert()"
webdriver.switchTo().alert().accept()

I called the javascript alert function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept() . 我调用了javascript alert功能,该功能将窗口置于前台,然后使用webdriver.switchTo().alert().accept()消除了警报。

Robot from java.awt; 来自java.awt的 机器人 could help you: 可以帮助您:

Robot.mouseMove(webDriver.manage().window().getPosition().getX(),webDriver.manage().window().getPosition().getY());
Robot.mousePress(InputEvent.BUTTON1_MASK);
Robot.mouseRelease(InputEvent.BUTTON1_MASK);

Limitations: 限制:

  • if you have more than one monitor, you need to calculate absolute coordinates. 如果您有多个监视器,则需要计算绝对坐标。 As webDriver.manage().window().getPosition().getX() give you offset for the currently used display. 由于webDriver.manage()。window()。getPosition()。getX()为您提供了当前使用的显示的偏移量。
  • window should be visible 窗口应该可见

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

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