java 如何关闭 Selenium 中所有打开的驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43090504/
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
How to close all opened drivers in Selenium?
提问by Stas Mackarow
I need to close all open Chrome drivers in Selenium. All my methods are closing only one of them. The reason why I need to close all drivers at the same time - in the start of my program I don't know how many drivers I need to open, so I try to open a few drivers with same driver names in cycle.
我需要关闭 Selenium 中所有打开的 Chrome 驱动程序。我所有的方法都只关闭其中一种。我需要同时关闭所有驱动程序的原因——在我的程序开始时我不知道我需要打开多少个驱动程序,所以我尝试循环打开几个驱动程序名称相同的驱动程序。
How I opened these drivers:
我如何打开这些驱动程序:
WebDriver driver = new ChromeDriver();
driver.get(firstURL);
driver = new ChromeDriver();
driver.get(secondURL);
How I tried to close both drivers:
我如何尝试关闭两个驱动程序:
First try:
第一次尝试:
driver.close();
Second try:
第二次尝试:
driver.quit();
Third try:
第三次尝试:
driver.close();
driver.close();
Fourth try:
第四次尝试:
driver.quit();
driver.quit();
回答by kushal.8
There is only a single WebDriver driver
in your Code, even though you are assigning it multiple ChromeDriver()
objects. So You can just close the driver once either by using driver.close()
or driver.quit();
WebDriver driver
即使您为它分配了多个ChromeDriver()
对象,您的代码中也只有一个。所以你可以通过使用driver.close()
或关闭驱动程序一次driver.quit();
and only the latest window will be closed all the previous windows will still remain (now which can't be contacted anymore) as the only driver was closed.
并且只有最新的窗口将关闭,所有以前的窗口仍将保留(现在无法再联系),因为唯一的驱动程序已关闭。
回答by SiKing
First let's look at your code:
首先让我们看看你的代码:
WebDriver driver = new ChromeDriver();
driver.get(firstURL);
driver = new ChromeDriver();
driver.get(secondURL);
Here is what is happening:
这是正在发生的事情:
- You open a new Chrome instance.
- You then do something with that instance: navigate to a website.
- The next step, several things are happening: you open a new Chrome instance, and you overwrite the referenceto the previous instance you opened. Essentially, at this point you lost track of the first browser you just opened!
- You now do something with the second opened instance: navigate to a website.
- 您打开一个新的 Chrome 实例。
- 然后您对该实例做一些事情:导航到一个网站。
- 下一步,发生了几件事情:您打开一个新的 Chrome 实例,并覆盖对您打开的前一个实例的引用。从本质上讲,此时您已经忘记了刚刚打开的第一个浏览器!
- 您现在对第二个打开的实例执行一些操作:导航到一个网站。
From you question it is not clear what exactly you are trying to accomplish. You have several options:
从你的问题来看,目前尚不清楚你到底想要完成什么。您有多种选择:
- Open one instance of a browser before your test. If you are using, for example JUnit, this is often done in the
@Before
method. - Do some work in your test.
- Close the browser after the test. Again in JUnit, this wold be done in the
@After
method.
- 在测试之前打开浏览器的一个实例。如果您正在使用,例如 JUnit,这通常在
@Before
方法中完成。 - 在你的测试中做一些工作。
- 测试后关闭浏览器。同样在 JUnit 中,这将在
@After
方法中完成。
Another alternative is that you may legitimately need multiple browsers. You will need to keep track of all of them.
另一种选择是您可能合法地需要多个浏览器。您将需要跟踪所有这些。
- You could create a
List
ofdrivers
, and every time you open a new one, add it to that list. - At the end of your tests, iterate over that list, and close all of them.
- 您可以创建一个
List
ofdrivers
,并且每次打开一个新的时,将其添加到该列表中。 - 在测试结束时,迭代该列表,并关闭所有这些列表。
回答by Zachary David Saunders
I have a couple suggestions for you.
我有几个建议给你。
1: Naming Convention
1:命名约定
In your post, you said:
在您的帖子中,您说:
"...in the start of my program I don't know how many drivers I need to open, so I try to open a few drivers with same driver names in cycle."
“...在我的程序开始时,我不知道需要打开多少个驱动程序,因此我尝试循环打开几个具有相同驱动程序名称的驱动程序。”
This is not a very good approach and here's why: let's say that you have two WebDrivers, each named "driverX". You need the first driver to go to "www.google.com", and you need the second driver to go to "www.yahoo.com". As such, your code looks like this:
这不是一个很好的方法,原因如下:假设您有两个 WebDriver,每个都命名为“driverX”。您需要第一个驱动程序才能访问“ www.google.com”,您需要第二个驱动程序才能访问“ www.yahoo.com”。因此,您的代码如下所示:
driverX.get("www.google.com");
driverX.get("www.yahoo.com");
This becomes a problem, as instead of telling the second WebDriver to go to Yahoo, you have actually rerouted the first WebDriver from Google to Yahoo.
这成为一个问题,因为您没有告诉第二个 WebDriver 转到 Yahoo,而是将第一个 WebDriver 从 Google 重新路由到 Yahoo。
What is the solution to this problem? Name each of your WebDrivers with a unique name (googleDriver
or yahooDriver
for example). Then, your code will look like this and you should not have any conflicts:
这个问题的解决方案是什么?使用唯一的名称(googleDriver
或yahooDriver
例如)为每个 WebDriver 命名。然后,您的代码将如下所示,您不应该有任何冲突:
googleDriver.get("www.google.com");
yahooDriver.get("www.yahoo.com");
2: WebDriver Grouping
2:WebDriver分组
Now that we have each of your WebDrivers with a unique name, we are able to reference them individually. This is great, however, some applications require that we reference several of them at once. This is where ArrayLists can come in. In the code below, I used an ArrayList to solve your original question.
现在我们拥有每个 WebDriver 的唯一名称,我们可以单独引用它们。这很好,但是,某些应用程序要求我们一次引用其中的几个。这是 ArrayLists 可以派上用场的地方。在下面的代码中,我使用 ArrayList 来解决您原来的问题。
ArrayList<WebDriver> activeDrivers = new ArrayList<>();
activeDrivers.add(googleDriver);
activeDrivers.add(yahooDriver);
for(WebDriver driver : activeDrivers){
if(driver.getCurrentUrl().equals("Some predefined exit page"){
driver.quit();
}
}
Feel free to leave a comment if you still do not understand or if you have any questions.
如果您仍然不明白或有任何疑问,请随时发表评论。
回答by optimistic_creeper
How did you initialize your driver. Did you initialize more than one driver? If so, then use quit()
method for all drivers separately. Otherwise, only driver.quit()
should work.
你是如何初始化你的驱动程序的。您是否初始化了多个驱动程序?如果是这样,则quit()
分别对所有驱动程序使用方法。否则,只能driver.quit()
工作。
Edit:
编辑:
Use driver.quit()
before each new assignment.
driver.quit()
在每次新分配之前使用。
回答by boooom
driver.quit() will close all (parent+child) browser windows and end the whole session. This should work fine.
driver.quit() 将关闭所有(父+子)浏览器窗口并结束整个会话。这应该可以正常工作。
回答by Suuresh
@AfterMethod
public void tearDown(){
// Your Code
// At the End call
driver.close();
}
回答by Leon
To expand on Zachary's answer, assuming you re using drivers in a test context, you can:
为了扩展 Zachary 的答案,假设您在测试环境中使用驱动程序,您可以:
- Create an arraylist in the test class
- add the drivers you create inside the test methods to this list
- add an @After method containing a for each loop calling quit() on all drivers dynamically added to the arraylist during tests
- 在测试类中创建一个数组列表
- 将您在测试方法中创建的驱动程序添加到此列表中
- 在测试期间动态添加到数组列表的所有驱动程序上添加一个包含对每个循环调用 quit() 的 @After 方法