- Selenium Webdriver Tutorial
- Home
- Introduction
- Installation
- Browser Navigation
- Identify Single Element
- Identify Multiple Elements
- Explicit and Implicit Wait
- Pop-ups
- Backward and Forward Navigation
- Cookies
- Exceptions
- Action Class
- Create a Basic Test
- Forms
- Drag and Drop
- Windows
- Alerts
- Handling Links
- Handling Edit Boxes
- Color Support
- Generating HTML Test Reports in Python
- Read/Write data from Excel
- Handling Checkboxes
- Executing Tests in Multiple Browsers
- Headless Execution
- Wait Support
- Select Support
- JavaScript Executor
- Chrome WebDriver Options
- Scroll Operations
- Capture Screenshots
- Right Click
- Double Click
- Selenium Webdriver Useful Resources
- Selenium WebDriver - Quick Guide
- Selenium WebDriver - Useful Resources
- Selenium WebDriver - Discussion
Selenium Webdriver - Pop-ups
A new pop-up window can open on clicking a link or a button. The webdriver by default has control over the main page, in order to access the elements on the new pop-up, the webdriver control has to be switched from the main page to the new pop-up window.
Methods
The methods to handle new pop-ups are listed below −
driver.current_window_handle − To obtain the handle id of the window in focus.
driver.window_handles − To obtain the list of all the opened window handle ids.
driver.swtich_to.window(< window handle id >) − To switch the webdriver control to an opened window whose handle id is passed as a parameter to the method.
On clicking the Sign in with Apple button, a new pop-up opens having the browser title as Sign in with Apple ID Let us try to switch to the new pop-up and access elements there.
Code Implementation
The code implementation for the pop-ups is as follows −
from selenium import webdriver driver = webdriver.Chrome(executable_path='../drivers/chromedriver') #implicit wait time driver.implicitly_wait(5) #url launch driver.get("https://the-internet.herokuapp.com/windows") #identify element s = driver.find_element_by_link_text("Click Here") s.click() #current main window handle m= driver.current_window_handle #iterate over all window handles for h in driver.window_handles: #check for main window handle if h != m: n = h #switch to new tab driver.switch_to.window(n) print('Page title of new tab: ' + driver.title) #switch to main window driver.switch_to.window(m) print('Page title of main window: ' + driver.title) #quit browser driver.quit()
Output
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. First the page title of the new pop-up(obtained from the method title) - Sign in with Apple ID gets printed in the console. Next, after switching the webdriver control to the main window, its page title - Sign In | Indeed Accounts get printed in the console.
To Continue Learning Please Login
Login with Google