Window And Tab Handel #11110
Unanswered
AkkiTester
asked this question in
Q&A
Replies: 1 comment
-
Converted to a discussion as this is not a pytest feature request. Please use discussions for questions like this. Also consider using proper code blocks so people can actually read your code easily. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
import time
"""from this code i want to say selenium consider both tab and window are same
in general tab and window is different things,
Tabs are contained within a single browser window,
Windows are separate instances of the Chrome browser running independently"""
from selenium import webdriver
from selenium.webdriver.common.by import By
Create a new instance of the browser driver
driver = webdriver.Chrome()
driver.get('https://www.google.com')
time.sleep(3)
Execute JavaScript to open a new window instance
driver.execute_script("window.open('https://www.example.com', 'new_window', 'width=800,height=600');")
time.sleep(3)
Execute JavaScript to open a new Tab
driver.execute_script("window.open();")
time.sleep(2)
Switch to the new window
driver.switch_to.window(driver.window_handles[1])
time.sleep(3)
Perform actions on the new window if needed
For example, let's get the title of the new window
new_window_title = driver.title
print("New window title:", new_window_title)
time.sleep(3)
window_handles = driver.window_handles
driver.switch_to.window(driver.window_handles[2])
print(window_handles)
Close the window
time.sleep(3)
Switch back to the original window
driver.switch_to.window(driver.window_handles[0])
print(driver.title)
time.sleep(3)
Switch to the window
driver.switch_to.window(driver.window_handles[1])
print(driver.title)
time.sleep(3)
Switch to the window
driver.switch_to.window(driver.window_handles[2])
print(driver.title)
time.sleep(3)
here selenium fail to handel I think . it can open new window but it gives deflection
driver.execute_script("window.open('https://the-internet.herokuapp.com/windows', 'new_window','width=700,height=500')")
time.sleep(3)
print(driver.title)
driver.find_element(By.XPATH, "//a[normalize-space()='Click Here']").click()
time.sleep(3)
print(driver.title)
print(window_handles)
time.sleep(2)
Close the driver
driver.quit()
What's the problem this feature will solve?
Describe the solution you'd like
Alternative Solutions
Additional context
Beta Was this translation helpful? Give feedback.
All reactions