@@ -19,7 +19,9 @@ current window by using:
19
19
20
20
{{< tabpane langEqualsHeader=true >}}
21
21
{{< badge-examples >}}
22
- {{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}}
22
+ {{< tab header="Java" text=true >}}
23
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
24
+ {{< /tab >}}
23
25
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
24
26
{{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}}
25
27
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
@@ -33,40 +35,15 @@ Clicking a link which opens in a
33
35
<a href =" https://seleniumhq.github.io " target =" _blank " > new window</a >
34
36
will focus the new window or tab on screen, but WebDriver will not know which
35
37
window the Operating System considers active. To work with the new window
36
- you will need to switch to it. If you have only two tabs or windows open,
37
- and you know which window you start with, by the process of elimination
38
- you can loop over both windows or tabs that WebDriver can see, and switch
39
- to the one which is not the original.
40
-
41
- However, Selenium 4 provides a new api [ NewWindow] ( #create-new-window-or-new-tab-and-switch )
42
- which creates a new tab (or) new window and automatically switches to it.
38
+ you will need to switch to it. For this, we fetch all window handles,
39
+ and store them in an array. The array position fills in the order the
40
+ window is launched. So first position will be default browser, and so on.
43
41
44
42
{{< tabpane langEqualsHeader=true >}}
45
43
{{< badge-examples >}}
46
- {{< tab header="Java" >}}
47
- //Store the ID of the original window
48
- String originalWindow = driver.getWindowHandle();
49
-
50
- //Check we don't have other windows open already
51
- assert driver.getWindowHandles().size() == 1;
52
-
53
- //Click the link which opens in a new window
54
- driver.findElement(By.linkText("new window")).click();
55
-
56
- //Wait for the new window or tab
57
- wait.until(numberOfWindowsToBe(2));
58
-
59
- //Loop through until we find a new window handle
60
- for (String windowHandle : driver.getWindowHandles()) {
61
- if(!originalWindow.contentEquals(windowHandle)) {
62
- driver.switchTo().window(windowHandle);
63
- break;
64
- }
65
- }
66
-
67
- //Wait for the new tab to finish loading content
68
- wait.until(titleIs("Selenium documentation"));
69
- {{< /tab >}}
44
+ {{< tab header="Java" text=true >}}
45
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}}
46
+ {{< /tab >}}
70
47
{{< tab header="Python" >}}
71
48
from selenium import webdriver
72
49
from selenium.webdriver.support.ui import WebDriverWait
@@ -204,60 +181,6 @@ wait.until(titleIs("Selenium documentation"))
204
181
{{< /tab >}}
205
182
{{< /tabpane >}}
206
183
207
- ### Create new window (or) new tab and switch
208
- Creates a new window (or) tab and will focus the new window or tab on screen.
209
- You don't need to switch to work with the new window (or) tab. If you have more than two windows
210
- (or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see,
211
- and switch to the one which is not the original.
212
-
213
- __ Note: This feature works with Selenium 4 and later versions.__
214
-
215
- {{< tabpane langEqualsHeader=true >}}
216
- {{< badge-examples >}}
217
- {{< tab header="Java" >}}
218
- // Opens a new tab and switches to new tab
219
- driver.switchTo().newWindow(WindowType.TAB);
220
-
221
- // Opens a new window and switches to new window
222
- driver.switchTo().newWindow(WindowType.WINDOW);
223
- {{< /tab >}}
224
- {{< tab header="Python" >}}
225
- # Opens a new tab and switches to new tab
226
- driver.switch_to.new_window('tab')
227
-
228
- # Opens a new window and switches to new window
229
- driver.switch_to.new_window('window')
230
- {{< /tab >}}
231
- {{< tab header="CSharp" >}}
232
- // Opens a new tab and switches to new tab
233
- driver.SwitchTo().NewWindow(WindowType.Tab)
234
-
235
- // Opens a new window and switches to new window
236
- driver.SwitchTo().NewWindow(WindowType.Window)
237
- {{< /tab >}}
238
- {{% tab header="Ruby" text=true %}}
239
- Opens a new tab and switches to new tab:
240
- {{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
241
-
242
- Opens a new window and switches to new window:
243
- {{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
244
- {{% /tab %}}
245
- {{< tab header="JavaScript" text=true >}}
246
- Opens a new tab and switches to new tab
247
- {{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}
248
-
249
- Opens a new window and switches to new window:
250
- {{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
251
- {{< /tab >}}
252
- {{< tab header="Kotlin" >}}
253
- // Opens a new tab and switches to new tab
254
- driver.switchTo().newWindow(WindowType.TAB)
255
-
256
- // Opens a new window and switches to new window
257
- driver.switchTo().newWindow(WindowType.WINDOW)
258
- {{< /tab >}}
259
- {{< /tabpane >}}
260
-
261
184
### Closing a window or tab
262
185
263
186
When you are finished with a window or tab _ and_ it is not the
@@ -268,13 +191,9 @@ handle stored in a variable. Put this together and you will get:
268
191
269
192
{{< tabpane langEqualsHeader=true >}}
270
193
{{< badge-examples >}}
271
- {{< tab header="Java" >}}
272
- //Close the tab or window
273
- driver.close();
274
-
275
- //Switch back to the old tab or window
276
- driver.switchTo().window(originalWindow);
277
- {{< /tab >}}
194
+ {{< tab header="Java" text=true >}}
195
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}}
196
+ {{< /tab >}}
278
197
{{< tab header="Python" >}}
279
198
#Close the tab or window
280
199
driver.close()
@@ -318,14 +237,68 @@ window will leave WebDriver executing on the now closed page, and will
318
237
trigger a ** No Such Window Exception** . You must switch
319
238
back to a valid window handle in order to continue execution.
320
239
240
+ ### Create new window (or) new tab and switch
241
+ Creates a new window (or) tab and will focus the new window or tab on screen.
242
+ You don't need to switch to work with the new window (or) tab. If you have more than two windows
243
+ (or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see,
244
+ and switch to the one which is not the original.
245
+
246
+ __ Note: This feature works with Selenium 4 and later versions.__
247
+
248
+ {{< tabpane langEqualsHeader=true >}}
249
+ {{< badge-examples >}}
250
+ {{< tab header="Java" text=true >}}
251
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}}
252
+ {{< /tab >}}
253
+ {{< tab header="Python" >}}
254
+ # Opens a new tab and switches to new tab
255
+ driver.switch_to.new_window('tab')
256
+
257
+ # Opens a new window and switches to new window
258
+ driver.switch_to.new_window('window')
259
+ {{< /tab >}}
260
+ {{< tab header="CSharp" >}}
261
+ // Opens a new tab and switches to new tab
262
+ driver.SwitchTo().NewWindow(WindowType.Tab)
263
+
264
+ // Opens a new window and switches to new window
265
+ driver.SwitchTo().NewWindow(WindowType.Window)
266
+ {{< /tab >}}
267
+ {{% tab header="Ruby" text=true %}}
268
+ Opens a new tab and switches to new tab:
269
+ {{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
270
+
271
+ Opens a new window and switches to new window:
272
+ {{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
273
+ {{% /tab %}}
274
+ {{< tab header="JavaScript" text=true >}}
275
+ Opens a new tab and switches to new tab
276
+ {{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}
277
+
278
+ Opens a new window and switches to new window:
279
+ {{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
280
+ {{< /tab >}}
281
+ {{< tab header="Kotlin" >}}
282
+ // Opens a new tab and switches to new tab
283
+ driver.switchTo().newWindow(WindowType.TAB)
284
+
285
+ // Opens a new window and switches to new window
286
+ driver.switchTo().newWindow(WindowType.WINDOW)
287
+ {{< /tab >}}
288
+ {{< /tabpane >}}
289
+
290
+
291
+
321
292
### Quitting the browser at the end of a session
322
293
323
294
When you are finished with the browser session you should call quit,
324
295
instead of close:
325
296
326
297
{{< tabpane langEqualsHeader=true >}}
327
298
{{< badge-examples >}}
328
- {{< tab header="Java" >}}driver.quit();{{< /tab >}}
299
+ {{< tab header="Java" text=true >}}
300
+ {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
301
+ {{< /tab >}}
329
302
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
330
303
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
331
304
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}
0 commit comments