dub
dub

Reputation: 1

python Selenium handling of pop-up window

I am using selenium to click through a website and automate a download, I am currently stumped on how to get selenium to switch to the pop-up window that is created when the download button is selected. In short, I want to interact with a pop-up window but am not familiar with the functions/syntax or process in doing this. Thanks!

Will update if I find a solution!

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
import time

# Path to Chrome executable (replace if necessary)
chrome = path
url = url

# Initialize WebDriver using ChromeDriverManager (recommended)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Open the desired URL
driver.get(url)

# Maximize the browser window
driver.maximize_window()

# Inform user to stop with CTRL+C
print("Press CTRL+C to stop program. Close Chrome Browser and stop Chrome Processes in Task Manager")

# Define elements in Base Window
iframe = '//*[@id="viz"]/iframe'
xpath1 = '/html/body/div[2]/div[2]/div[1]/div[2]/div[2]/div[5]/button'
xpath2 = '/html/body/div[6]/div/div/div/div[2]/div'
xpath3 = ''

# Define elements in Pop-Up Window

# Infinite loop checking for element (potentially inefficient)
try:
    # Switch to the iframe containing the element
    # Wait for the iframe to be present
    iframe = WebDriverWait(driver, 15).until(
        EC.presence_of_element_located((By.XPATH, iframe))
    )

    # Switch to the iframe containing the element
    driver.switch_to.frame(iframe)

    # Explicitly wait for the element within the iframe
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, xpath1))
    )

    # Click the element
    element.click()
    
    # Explicitly wait for the element within the iframe
    element2 = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, xpath2))
    )

    # Click the element
    element2.click()
    
    # Move back from iframe to parent HTML
    driver.switch_to.default_content()

    
except TimeoutException:
    # Handle timeout gracefully, e.g., log a message or retry
    print("Element not found. Retrying...")
    time.sleep(10)  # Adjust retry wait time as needed

# Move back from iframe to parent HTML
driver.switch_to.default_content()

Upvotes: 0

Views: 285

Answers (1)

dub
dub

Reputation: 1

It was lengthy, but I was able to find a solution and am now able to interact with the pop-up window. I also should note that the pop-up windows url is dynamic:

    # Wait for the pop-up window to appear
while len(driver.window_handles) == 1:
    time.sleep(1)  # Adjust wait time as needed

# Store the original window handle
base_window = driver.current_window_handle

# Iterate through all available window handles
for window_handle in driver.window_handles:
    if window_handle != base_window:
        # Switch to the new window
        driver.switch_to.window(window_handle)

        # Get and print the URL of the current window
        current_url = driver.current_url
        print("Pop-up window URL:", current_url)

        # Switch back to the original window
        driver.switch_to.window(base_window)
        break  # Exit the loop after finding and printing the URL
        
# Switch to the pop-up window using its title or URL
new_window_title = current_url  # Replace with the expected title of the pop-up window
# new_window_url = "https://www.example.com/popup"  # Replace with the pop-up window URL (if known)

while True:
    if len(driver.window_handles) > 1:
        # Check if a new window has appeared
        for window_handle in driver.window_handles:
            if window_handle != base:
                # Switch to the new window
                driver.switch_to.window(window_handle)
                
                # Verify if the title or URL matches the expected pop-up
                if new_window_title in driver.current_url:
                    # Success! We're now in the pop-up window
                    break
                else:
                    # Close the unexpected window and continue checking
                    driver.close()
    else:
        # No new window found, wait and check again
        time.sleep(2)  # Adjust wait time as needed

Upvotes: 0

Related Questions