From 048e7e79cf4ba03f3216e4f411289493117d2f1f Mon Sep 17 00:00:00 2001 From: pallavigitwork Date: Wed, 12 Jun 2024 15:03:44 +0530 Subject: [PATCH 1/2] added example code for windows and switched text order. modified some text also --- .../selenium/interactions/WindowsTest.java | 47 ++++- .../webdriver/interactions/windows.en.md | 161 ++++++++---------- .../webdriver/interactions/windows.ja.md | 148 +++++++--------- .../webdriver/interactions/windows.pt-br.md | 150 +++++++--------- .../webdriver/interactions/windows.zh-cn.md | 145 +++++++--------- 5 files changed, 299 insertions(+), 352 deletions(-) diff --git a/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java b/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java index 2e686aa0fb6a..0ea2402da943 100644 --- a/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java +++ b/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java @@ -1,7 +1,48 @@ package dev.selenium.interactions; -import dev.selenium.BaseTest; +import org.openqa.selenium.*; +import org.openqa.selenium.chrome.ChromeDriver; +import java.time.Duration; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; -public class WindowsTest extends BaseTest { +public class WindowsTest { -} + @Test + public void windowsExampleCode() { + + WebDriver driver = new ChromeDriver(); + driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); + // Navigate to Url + driver.get("https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html"); + //fetch handle of this + String currHandle=driver.getWindowHandle(); + assertNotNull(currHandle); + + //click on link to open a new window + driver.findElement(By.linkText("Open new window")).click(); + //fetch handles of all windows, there will be two, [0]- default, [1] - new window + Object[] windowHandles=driver.getWindowHandles().toArray(); + driver.switchTo().window((String) windowHandles[1]); + //assert on title of new window + String title=driver.getTitle(); + assertEquals("Simple Page",title); + + //closing current window + driver.close(); + //Switch back to the old tab or window + driver.switchTo().window((String) windowHandles[0]); + + //Opens a new tab and switches to new tab + driver.switchTo().newWindow(WindowType.TAB); + assertEquals("",driver.getTitle()); + + //Opens a new window and switches to new window + driver.switchTo().newWindow(WindowType.WINDOW); + assertEquals("",driver.getTitle()); + + //quitting driver + driver.quit(); //close all windows + + } +} \ No newline at end of file diff --git a/website_and_docs/content/documentation/webdriver/interactions/windows.en.md b/website_and_docs/content/documentation/webdriver/interactions/windows.en.md index 6dfd31bc4ff2..400daf594aa7 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/windows.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/windows.en.md @@ -19,7 +19,9 @@ current window by using: {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}} +{{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.current_window_handle{{< /tab >}} {{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}} {{< tab header="Ruby" >}}driver.window_handle{{< /tab >}} @@ -33,40 +35,15 @@ Clicking a link which opens in a new window will focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window -you will need to switch to it. If you have only two tabs or windows open, -and you know which window you start with, by the process of elimination -you can loop over both windows or tabs that WebDriver can see, and switch -to the one which is not the original. - -However, Selenium 4 provides a new api [NewWindow](#create-new-window-or-new-tab-and-switch) -which creates a new tab (or) new window and automatically switches to it. +you will need to switch to it. For this, we fetch all window handles, +and store them in an array. The array position fills in the order the +window is launched. So first position will be default browser, and so on. {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Store the ID of the original window -String originalWindow = driver.getWindowHandle(); - -//Check we don't have other windows open already -assert driver.getWindowHandles().size() == 1; - -//Click the link which opens in a new window -driver.findElement(By.linkText("new window")).click(); - -//Wait for the new window or tab -wait.until(numberOfWindowsToBe(2)); - -//Loop through until we find a new window handle -for (String windowHandle : driver.getWindowHandles()) { - if(!originalWindow.contentEquals(windowHandle)) { - driver.switchTo().window(windowHandle); - break; - } -} - -//Wait for the new tab to finish loading content -wait.until(titleIs("Selenium documentation")); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}} +{{< /tab >}} {{< tab header="Python" >}} from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait @@ -204,60 +181,6 @@ wait.until(titleIs("Selenium documentation")) {{< /tab >}} {{< /tabpane >}} -### Create new window (or) new tab and switch -Creates a new window (or) tab and will focus the new window or tab on screen. -You don't need to switch to work with the new window (or) tab. If you have more than two windows -(or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see, -and switch to the one which is not the original. - -__Note: This feature works with Selenium 4 and later versions.__ - -{{< tabpane langEqualsHeader=true >}} -{{< badge-examples >}} - {{< tab header="Java" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB); - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW); - {{< /tab >}} - {{< tab header="Python" >}} - # Opens a new tab and switches to new tab -driver.switch_to.new_window('tab') - - # Opens a new window and switches to new window -driver.switch_to.new_window('window') - {{< /tab >}} - {{< tab header="CSharp" >}} -// Opens a new tab and switches to new tab -driver.SwitchTo().NewWindow(WindowType.Tab) - -// Opens a new window and switches to new window -driver.SwitchTo().NewWindow(WindowType.Window) - {{< /tab >}} - {{% tab header="Ruby" text=true %}} -Opens a new tab and switches to new tab: -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} - -Opens a new window and switches to new window: -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} - {{% /tab %}} -{{< tab header="JavaScript" text=true >}} -Opens a new tab and switches to new tab -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} - -Opens a new window and switches to new window: -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} -{{< /tab >}} - {{< tab header="Kotlin" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB) - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW) - {{< /tab >}} -{{< /tabpane >}} - ### Closing a window or tab 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: {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}} -//Close the tab or window -driver.close(); - -//Switch back to the old tab or window -driver.switchTo().window(originalWindow); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}} +{{< /tab >}} {{< tab header="Python" >}} #Close the tab or window driver.close() @@ -318,6 +237,58 @@ window will leave WebDriver executing on the now closed page, and will trigger a **No Such Window Exception**. You must switch back to a valid window handle in order to continue execution. +### Create new window (or) new tab and switch +Creates a new window (or) tab and will focus the new window or tab on screen. +You don't need to switch to work with the new window (or) tab. If you have more than two windows +(or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see, +and switch to the one which is not the original. + +__Note: This feature works with Selenium 4 and later versions.__ + +{{< tabpane langEqualsHeader=true >}} +{{< badge-examples >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}} +{{< /tab >}} + {{< tab header="Python" >}} + # Opens a new tab and switches to new tab +driver.switch_to.new_window('tab') + + # Opens a new window and switches to new window +driver.switch_to.new_window('window') + {{< /tab >}} + {{< tab header="CSharp" >}} +// Opens a new tab and switches to new tab +driver.SwitchTo().NewWindow(WindowType.Tab) + +// Opens a new window and switches to new window +driver.SwitchTo().NewWindow(WindowType.Window) + {{< /tab >}} + {{% tab header="Ruby" text=true %}} +Opens a new tab and switches to new tab: +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} + +Opens a new window and switches to new window: +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} + {{% /tab %}} +{{< tab header="JavaScript" text=true >}} +Opens a new tab and switches to new tab +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} + +Opens a new window and switches to new window: +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} +{{< /tab >}} + {{< tab header="Kotlin" >}} +// Opens a new tab and switches to new tab +driver.switchTo().newWindow(WindowType.TAB) + +// Opens a new window and switches to new window +driver.switchTo().newWindow(WindowType.WINDOW) + {{< /tab >}} +{{< /tabpane >}} + + + ### Quitting the browser at the end of a session When you are finished with the browser session you should call quit, @@ -325,7 +296,9 @@ instead of close: {{< tabpane langEqualsHeader=true >}} {{< badge-examples >}} - {{< tab header="Java" >}}driver.quit();{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.quit(){{< /tab >}} {{< tab header="CSharp" >}}driver.Quit();{{< /tab >}} {{< tab header="Ruby" >}}driver.quit{{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md b/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md index 088c7141d17d..4e3d55c68a7b 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md @@ -17,7 +17,9 @@ WebDriverは、ウィンドウとタブを区別しません。 次のコードを使用して、現在のウィンドウのウィンドウハンドルを取得できます。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.current_window_handle{{< /tab >}} {{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}} {{< tab header="Ruby" >}}driver.window_handle{{< /tab >}} @@ -34,30 +36,9 @@ WebDriverは、ウィンドウとタブを区別しません。 ただし、Selenium 4には、新しいタブ(または)新しいウィンドウを作成して自動的に切り替える新しいAPI [NewWindow](#新しいウィンドウまたは新しいタブを作成して切り替える) が用意されています。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Store the ID of the original window -String originalWindow = driver.getWindowHandle(); - -//Check we don't have other windows open already -assert driver.getWindowHandles().size() == 1; - -//Click the link which opens in a new window -driver.findElement(By.linkText("new window")).click(); - -//Wait for the new window or tab -wait.until(numberOfWindowsToBe(2)); - -//Loop through until we find a new window handle -for (String windowHandle : driver.getWindowHandles()) { - if(!originalWindow.contentEquals(windowHandle)) { - driver.switchTo().window(windowHandle); - break; - } -} - -//Wait for the new tab to finish loading content -wait.until(titleIs("Selenium documentation")); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}} +{{< /tab >}} {{< tab header="Python" >}} from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait @@ -195,59 +176,6 @@ wait.until(titleIs("Selenium documentation")) {{< /tab >}} {{< /tabpane >}} -### 新しいウィンドウ(または)新しいタブを作成して切り替える - -新しいウィンドウ(または)タブを作成し、画面上の新しいウィンドウまたはタブにフォーカスします。 -新しいウィンドウ(または)タブを使用するように切り替える必要はありません。 -新しいウィンドウ以外に3つ以上のウィンドウ(または)タブを開いている場合、WebDriverが表示できる両方のウィンドウまたはタブをループして、元のものではないものに切り替えることができます。 - -__注意: この機能は、Selenium 4以降のバージョンで機能します。__ - -{{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB); - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW); - {{< /tab >}} - {{< tab header="Python" >}} - # Opens a new tab and switches to new tab -driver.switch_to.new_window('tab') - - # Opens a new window and switches to new window -driver.switch_to.new_window('window') - {{< /tab >}} - {{< tab header="CSharp" >}} -// Opens a new tab and switches to new tab -driver.SwitchTo().NewWindow(WindowType.Tab) - -// Opens a new window and switches to new window -driver.SwitchTo().NewWindow(WindowType.Window) - {{< /tab >}} - {{% tab header="Ruby" text=true %}} -Opens a new tab and switches to new tab -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} - -Opens a new window and switches to new window -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} - {{% /tab %}} -{{< tab header="JavaScript" text=true >}} -Opens a new tab and switches to new tab -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} - -Opens a new window and switches to new window: -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} -{{< /tab >}} - {{< tab header="Kotlin" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB) - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW) - {{< /tab >}} -{{< /tabpane >}} - ### ウィンドウまたはタブを閉じる ウィンドウまたはタブでの作業が終了し、 _かつ_ ブラウザーで最後に開いたウィンドウまたはタブではない場合、それを閉じて、以前使用していたウィンドウに切り替える必要があります。 @@ -255,13 +183,9 @@ driver.switchTo().newWindow(WindowType.WINDOW) これをまとめると以下のようになります。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Close the tab or window -driver.close(); - -//Switch back to the old tab or window -driver.switchTo().window(originalWindow); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}} +{{< /tab >}} {{< tab header="Python" >}} #Close the tab or window driver.close() @@ -302,12 +226,64 @@ driver.switchTo().window(originalWindow) ウィンドウを閉じた後に別のウィンドウハンドルに切り替えるのを忘れると、現在閉じられているページでWebDriverが実行されたままになり、 **No Such Window Exception** が発行されます。実行を継続するには、有効なウィンドウハンドルに切り替える必要があります。 +### 新しいウィンドウ(または)新しいタブを作成して切り替える + +新しいウィンドウ(または)タブを作成し、画面上の新しいウィンドウまたはタブにフォーカスします。 +新しいウィンドウ(または)タブを使用するように切り替える必要はありません。 +新しいウィンドウ以外に3つ以上のウィンドウ(または)タブを開いている場合、WebDriverが表示できる両方のウィンドウまたはタブをループして、元のものではないものに切り替えることができます。 + +__注意: この機能は、Selenium 4以降のバージョンで機能します。__ + +{{< tabpane langEqualsHeader=true >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}} +{{< /tab >}} + {{< tab header="Python" >}} + # Opens a new tab and switches to new tab +driver.switch_to.new_window('tab') + + # Opens a new window and switches to new window +driver.switch_to.new_window('window') + {{< /tab >}} + {{< tab header="CSharp" >}} +// Opens a new tab and switches to new tab +driver.SwitchTo().NewWindow(WindowType.Tab) + +// Opens a new window and switches to new window +driver.SwitchTo().NewWindow(WindowType.Window) + {{< /tab >}} + {{% tab header="Ruby" text=true %}} +Opens a new tab and switches to new tab +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} + +Opens a new window and switches to new window +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} + {{% /tab %}} +{{< tab header="JavaScript" text=true >}} +Opens a new tab and switches to new tab +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} + +Opens a new window and switches to new window: +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} +{{< /tab >}} + {{< tab header="Kotlin" >}} +// Opens a new tab and switches to new tab +driver.switchTo().newWindow(WindowType.TAB) + +// Opens a new window and switches to new window +driver.switchTo().newWindow(WindowType.WINDOW) + {{< /tab >}} +{{< /tabpane >}} + + ### セッションの終了時にブラウザーを終了する ブラウザーセッションを終了したら、closeではなく、quitを呼び出す必要があります。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}}driver.quit();{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.quit(){{< /tab >}} {{< tab header="CSharp" >}}driver.Quit();{{< /tab >}} {{< tab header="Ruby" >}}driver.quit{{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md b/website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md index 21634a3f0465..f60df6464774 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md @@ -17,7 +17,9 @@ usando um identificador. Cada janela tem um identificador único que permanece persistente em uma única sessão. Você pode pegar o identificador atual usando: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.current_window_handle{{< /tab >}} {{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}} {{< tab header="Ruby" >}}driver.window_handle{{< /tab >}} @@ -40,30 +42,9 @@ No entanto, o Selenium 4 fornece uma nova API [NewWindow](#criar-nova-janela-ou- que cria uma nova guia (ou) nova janela e muda automaticamente para ela. {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Store the ID of the original window -String originalWindow = driver.getWindowHandle(); - -//Check we don't have other windows open already -assert driver.getWindowHandles().size() == 1; - -//Click the link which opens in a new window -driver.findElement(By.linkText("new window")).click(); - -//Wait for the new window or tab -wait.until(numberOfWindowsToBe(2)); - -//Loop through until we find a new window handle -for (String windowHandle : driver.getWindowHandles()) { - if(!originalWindow.contentEquals(windowHandle)) { - driver.switchTo().window(windowHandle); - break; - } -} - -//Wait for the new tab to finish loading content -wait.until(titleIs("Selenium documentation")); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}} +{{< /tab >}} {{< tab header="Python" >}} from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait @@ -201,59 +182,6 @@ wait.until(titleIs("Selenium documentation")) {{< /tab >}} {{< /tabpane >}} -### Criar nova janela (ou) nova guia e alternar -Cria uma nova janela (ou) guia e focará a nova janela ou guia na tela. -Você não precisa mudar para trabalhar com a nova janela (ou) guia. Se você tiver mais de duas janelas -(ou) guias abertas diferentes da nova janela, você pode percorrer as janelas ou guias que o WebDriver pode ver -e mudar para aquela que não é a original. - -__Nota: este recurso funciona com Selenium 4 e versões posteriores.__ - -{{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB); - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW); - {{< /tab >}} - {{< tab header="Python" >}} - # Opens a new tab and switches to new tab -driver.switch_to.new_window('tab') - - # Opens a new window and switches to new window -driver.switch_to.new_window('window') - {{< /tab >}} - {{< tab header="CSharp" >}} -// Opens a new tab and switches to new tab -driver.SwitchTo().NewWindow(WindowType.Tab) - -// Opens a new window and switches to new window -driver.SwitchTo().NewWindow(WindowType.Window) - {{< /tab >}} - {{% tab header="Ruby" text=true %}} -Opens a new tab and switches to new tab -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} - -Opens a new window and switches to new window -{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} - {{% /tab %}} -{{< tab header="JavaScript" text=true >}} -Opens a new tab and switches to new tab -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} - -Opens a new window and switches to new window: -{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} -{{< /tab >}} - {{< tab header="Kotlin" >}} -// Opens a new tab and switches to new tab -driver.switchTo().newWindow(WindowType.TAB) - -// Opens a new window and switches to new window -driver.switchTo().newWindow(WindowType.WINDOW) - {{< /tab >}} -{{< /tabpane >}} - ### Fechando uma janela ou guia Quando você fechar uma janela ou guia _e_ que não é a @@ -263,13 +191,9 @@ amostra de código na seção anterior, você terá o identificador da janela anterior armazenado em uma variável. Junte isso e você obterá: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}} -//Close the tab or window -driver.close(); - -//Switch back to the old tab or window -driver.switchTo().window(originalWindow); - {{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}} +{{< /tab >}} {{< tab header="Python" >}} #Close the tab or window driver.close() @@ -312,6 +236,56 @@ Esquecer de voltar para outro gerenciador de janela após fechar uma janela deixará o WebDriver em execução na página agora fechada e acionara uma **No Such Window Exception**. Você deve trocar de volta para um identificador de janela válido para continuar a execução. +### Criar nova janela (ou) nova guia e alternar +Cria uma nova janela (ou) guia e focará a nova janela ou guia na tela. +Você não precisa mudar para trabalhar com a nova janela (ou) guia. Se você tiver mais de duas janelas +(ou) guias abertas diferentes da nova janela, você pode percorrer as janelas ou guias que o WebDriver pode ver +e mudar para aquela que não é a original. + +__Nota: este recurso funciona com Selenium 4 e versões posteriores.__ + +{{< tabpane langEqualsHeader=true >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}} +{{< /tab >}} + {{< tab header="Python" >}} + # Opens a new tab and switches to new tab +driver.switch_to.new_window('tab') + + # Opens a new window and switches to new window +driver.switch_to.new_window('window') + {{< /tab >}} + {{< tab header="CSharp" >}} +// Opens a new tab and switches to new tab +driver.SwitchTo().NewWindow(WindowType.Tab) + +// Opens a new window and switches to new window +driver.SwitchTo().NewWindow(WindowType.Window) + {{< /tab >}} + {{% tab header="Ruby" text=true %}} +Opens a new tab and switches to new tab +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}} + +Opens a new window and switches to new window +{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}} + {{% /tab %}} +{{< tab header="JavaScript" text=true >}} +Opens a new tab and switches to new tab +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}} + +Opens a new window and switches to new window: +{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}} +{{< /tab >}} + {{< tab header="Kotlin" >}} +// Opens a new tab and switches to new tab +driver.switchTo().newWindow(WindowType.TAB) + +// Opens a new window and switches to new window +driver.switchTo().newWindow(WindowType.WINDOW) + {{< /tab >}} +{{< /tabpane >}} + + ### Sair do navegador no final de uma sessão @@ -319,7 +293,11 @@ Quando você terminar a sessão do navegador, você deve chamar a função _quit em vez de fechar: {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" >}}driver.quit();{{< /tab >}} + + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}} +{{< /tab >}} + {{< tab header="Python" >}}driver.quit(){{< /tab >}} {{< tab header="CSharp" >}}driver.Quit();{{< /tab >}} {{< tab header="Ruby" >}}driver.quit{{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/windows.zh-cn.md b/website_and_docs/content/documentation/webdriver/interactions/windows.zh-cn.md index 43e024fe3eda..8ead23278f5b 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/windows.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/interactions/windows.zh-cn.md @@ -13,7 +13,9 @@ WebDriver 没有区分窗口和标签页。如果你的站点打开了一个新 每个窗口都有一个唯一的标识符,该标识符在单个会话中保持持久性。你可以使用以下方法获得当前窗口的窗口句柄: {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}} +{{< /tab >}} {{< tab header="Python" >}}driver.current_window_handle{{< /tab >}} {{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}} {{< tab header="Ruby" >}}driver.window_handle{{< /tab >}} @@ -33,29 +35,8 @@ WebDriver 没有区分窗口和标签页。如果你的站点打开了一个新 它创建一个新选项卡 (或) 新窗口并自动切换到它。 {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 存储原始窗口的 ID -String originalWindow = driver.getWindowHandle(); - -// 检查一下,我们还没有打开其他的窗口 -assert driver.getWindowHandles().size() == 1; - -// 点击在新窗口中打开的链接 -driver.findElement(By.linkText("new window")).click(); - -// 等待新窗口或标签页 -wait.until(numberOfWindowsToBe(2)); - -// 循环执行,直到找到一个新的窗口句柄 -for (String windowHandle : driver.getWindowHandles()) { - if(!originalWindow.contentEquals(windowHandle)) { - driver.switchTo().window(windowHandle); - break; - } -} - -// 等待新标签完成加载内容 -wait.until(titleIs("Selenium documentation")); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}} {{< /tab >}} {{< tab header="Python" >}} from selenium import webdriver @@ -189,6 +170,56 @@ wait.until(titleIs("Selenium documentation")) {{< /tab >}} {{< /tabpane >}} +### 关闭窗口或标签页 + +当你完成了一个窗口或标签页的工作时,_并且_它不是浏览器中最后一个打开的窗口或标签页时,你应该关闭它并切换回你之前使用的窗口。 +假设您遵循了前一节中的代码示例,您将把前一个窗口句柄存储在一个变量中。把这些放在一起,你会得到: + +{{< tabpane langEqualsHeader=true >}} + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}} +{{< /tab >}} +{{< tab header="Python" >}} + #关闭标签页或窗口 +driver.close() + + #切回到之前的标签页或窗口 +driver.switch_to.window(original_window) +{{< /tab >}} +{{< tab header="CSharp" >}} +//关闭标签页或窗口 +driver.Close(); + +//切回到之前的标签页或窗口 +driver.SwitchTo().Window(originalWindow); +{{< /tab >}} +{{< tab header="Ruby" >}} + #关闭标签页或窗口 +driver.close + + #切回到之前的标签页或窗口 +driver.switch_to.window original_window +{{< /tab >}} +{{< tab header="JavaScript" >}} +//关闭标签页或窗口 +await driver.close(); + +//切回到之前的标签页或窗口 +await driver.switchTo().window(originalWindow); +{{< /tab >}} +{{< tab header="Kotlin" >}} +//关闭标签页或窗口 +driver.close() + +//切回到之前的标签页或窗口 +driver.switchTo().window(originalWindow) + +{{< /tab >}} +{{< /tabpane >}} + +如果在关闭一个窗口后忘记切换回另一个窗口句柄,WebDriver 将在当前关闭的页面上执行,并触发一个 +**No Such Window Exception 无此窗口异常**。必须切换回有效的窗口句柄才能继续执行。 + ### 创建新窗口(或)新标签页并且切换 创建一个新窗口 (或) 标签页,屏幕焦点将聚焦在新窗口或标签在上。您不需要切换到新窗口 (或) 标签页。如果除了新窗口之外, @@ -197,12 +228,8 @@ wait.until(titleIs("Selenium documentation")) _注意: 该特性适用于 Selenium 4 及其后续版本。_ {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -// 打开新标签页并切换到新标签页 -driver.switchTo().newWindow(WindowType.TAB); - -// 打开一个新窗口并切换到新窗口 -driver.switchTo().newWindow(WindowType.WINDOW); + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}} {{< /tab >}} {{< tab header="Python" >}} # 打开新标签页并切换到新标签页 @@ -242,65 +269,17 @@ driver.switchTo().newWindow(WindowType.WINDOW) {{< /tab >}} {{< /tabpane >}} -### 关闭窗口或标签页 - -当你完成了一个窗口或标签页的工作时,_并且_它不是浏览器中最后一个打开的窗口或标签页时,你应该关闭它并切换回你之前使用的窗口。 -假设您遵循了前一节中的代码示例,您将把前一个窗口句柄存储在一个变量中。把这些放在一起,你会得到: - -{{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}} -//关闭标签页或窗口 -driver.close(); - -//切回到之前的标签页或窗口 -driver.switchTo().window(originalWindow); -{{< /tab >}} -{{< tab header="Python" >}} - #关闭标签页或窗口 -driver.close() - #切回到之前的标签页或窗口 -driver.switch_to.window(original_window) -{{< /tab >}} -{{< tab header="CSharp" >}} -//关闭标签页或窗口 -driver.Close(); - -//切回到之前的标签页或窗口 -driver.SwitchTo().Window(originalWindow); -{{< /tab >}} -{{< tab header="Ruby" >}} - #关闭标签页或窗口 -driver.close - - #切回到之前的标签页或窗口 -driver.switch_to.window original_window -{{< /tab >}} -{{< tab header="JavaScript" >}} -//关闭标签页或窗口 -await driver.close(); - -//切回到之前的标签页或窗口 -await driver.switchTo().window(originalWindow); -{{< /tab >}} -{{< tab header="Kotlin" >}} -//关闭标签页或窗口 -driver.close() - -//切回到之前的标签页或窗口 -driver.switchTo().window(originalWindow) - -{{< /tab >}} -{{< /tabpane >}} - -如果在关闭一个窗口后忘记切换回另一个窗口句柄,WebDriver 将在当前关闭的页面上执行,并触发一个 -**No Such Window Exception 无此窗口异常**。必须切换回有效的窗口句柄才能继续执行。 ### 在会话结束时退出浏览器 当你完成了浏览器会话,你应该调用 quit 退出,而不是 close 关闭: {{< tabpane langEqualsHeader=true >}} -{{< tab header="Java" >}}driver.quit();{{< /tab >}} + + {{< tab header="Java" text=true >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}} +{{< /tab >}} + {{< tab header="Python" >}}driver.quit(){{< /tab >}} {{< tab header="CSharp" >}}driver.Quit();{{< /tab >}} {{< tab header="Ruby" >}}driver.quit{{< /tab >}} From 9a0da9623f396b97874aa6d3d31e054f8f2cadf3 Mon Sep 17 00:00:00 2001 From: pallavigitwork Date: Wed, 12 Jun 2024 15:10:24 +0530 Subject: [PATCH 2/2] fixed tab pane hugo error --- .../content/documentation/webdriver/interactions/windows.ja.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md b/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md index 4e3d55c68a7b..8a754a3567d2 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/windows.ja.md @@ -281,7 +281,7 @@ driver.switchTo().newWindow(WindowType.WINDOW) ブラウザーセッションを終了したら、closeではなく、quitを呼び出す必要があります。 {{< tabpane langEqualsHeader=true >}} - {{< tab header="Java" text=true >}} + {{< tab header="Java" text=true >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}} {{< /tab >}} {{< tab header="Python" >}}driver.quit(){{< /tab >}}