Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/python/tests/interactions/test_windows.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
import pytest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

url = "https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html"

@pytest.fixture()
def driver():
driver = webdriver.Chrome()
yield driver
driver.quit()

def test_current_window_handle(driver):
driver.get(url)
current_handle = driver.current_window_handle
assert current_handle is not None


def test_switch_to_window(driver):
driver.get(url)
wait = WebDriverWait(driver, 10)

original_window_handle = driver.current_window_handle

driver.find_element(By.LINK_TEXT, "Open new window").click()
wait.until(EC.number_of_windows_to_be(2))

new_window_handle = (set(driver.window_handles) - {original_window_handle}).pop()
driver.switch_to.window(new_window_handle)
assert driver.current_window_handle == new_window_handle

def test_close_window(driver):
driver.get(url)
wait = WebDriverWait(driver, 10)

original_window_handle = driver.current_window_handle

driver.find_element(By.LINK_TEXT, "Open new window").click()
wait.until(EC.number_of_windows_to_be(2))

new_window_handle = (set(driver.window_handles) - {original_window_handle}).pop()
driver.switch_to.window(new_window_handle)

driver.close()
driver.switch_to.window(original_window_handle)

assert driver.current_window_handle == original_window_handle
assert len(driver.window_handles) == 1

def test_create_new_window(driver):
# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')
assert driver.title == ""
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
assert driver.title == ""
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ current window by using:
{{< 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="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
Expand All @@ -46,38 +48,8 @@ window is launched. So first position will be default browser, and so on.
{{< 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
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Firefox() as driver:
# Open URL
driver.get("https://seleniumhq.github.io")

# Setup wait for later
wait = WebDriverWait(driver, 10)

# Store the ID of the original window
original_window = driver.current_window_handle

# Check we don't have other windows open already
assert len(driver.window_handles) == 1

# Click the link which opens in a new window
driver.find_element(By.LINK_TEXT, "new window").click()

# Wait for the new window or tab
wait.until(EC.number_of_windows_to_be(2))

# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window(window_handle)
break

# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -176,12 +148,8 @@ handle stored in a variable. Put this together and you will get:
{{< 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()

#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -230,12 +198,8 @@ __Note: This feature works with Selenium 4 and later versions.__
{{< 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 header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
{{< /tab >}}


Expand Down Expand Up @@ -278,7 +242,9 @@ instead of close:
{{< 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="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ WebDriverは、ウィンドウとタブを区別しません。
{{< 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="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
Expand All @@ -41,39 +43,8 @@ WebDriverは、ウィンドウとタブを区別しません。
{{< 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
from selenium.webdriver.support import expected_conditions as EC

# Start the driver
with webdriver.Firefox() as driver:
# Open URL
driver.get("https://seleniumhq.github.io")

# Setup wait for later
wait = WebDriverWait(driver, 10)

# Store the ID of the original window
original_window = driver.current_window_handle

# Check we don't have other windows open already
assert len(driver.window_handles) == 1

# Click the link which opens in a new window
driver.find_element(By.LINK_TEXT, "new window").click()

# Wait for the new window or tab
wait.until(EC.number_of_windows_to_be(2))

# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window(window_handle)
break

# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -169,12 +140,8 @@ wait.until(titleIs("Selenium documentation"))
{{< 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()

#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -220,12 +187,8 @@ __注意: この機能は、Selenium 4以降のバージョンで機能します
{{< 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 header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -268,7 +231,9 @@ driver.switchTo().newWindow(WindowType.WINDOW)
{{< 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="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
{{< /tab >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ persistente em uma única sessão. Você pode pegar o identificador atual usando
{{< 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="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
{{< /tab >}}
Expand All @@ -47,39 +49,8 @@ que cria uma nova guia (ou) nova janela e muda automaticamente para ela.
{{< 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
from selenium.webdriver.support import expected_conditions as EC

# Start the driver
with webdriver.Firefox() as driver:
# Open URL
driver.get("https://seleniumhq.github.io")

# Setup wait for later
wait = WebDriverWait(driver, 10)

# Store the ID of the original window
original_window = driver.current_window_handle

# Check we don't have other windows open already
assert len(driver.window_handles) == 1

# Click the link which opens in a new window
driver.find_element(By.LINK_TEXT, "new window").click()

# Wait for the new window or tab
wait.until(EC.number_of_windows_to_be(2))

# Loop through until we find a new window handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window(window_handle)
break

# Wait for the new tab to finish loading content
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -178,12 +149,8 @@ anterior armazenado em uma variável. Junte isso e você obterá:
{{< 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()

#Switch back to the old tab or window
driver.switch_to.window(original_window)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -230,12 +197,8 @@ __Nota: este recurso funciona com Selenium 4 e versões posteriores.__
{{< 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 header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
{{< /tab >}}

{{< tab header="CSharp" text=true >}}
Expand Down Expand Up @@ -279,7 +242,9 @@ em vez de fechar:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
{{< /tab >}}

{{< tab header="Python" >}}driver.quit(){{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
{{< /tab >}}
{{< tab header="CSharp" text=true >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
{{< /tab >}}
Expand Down
Loading
Loading