sera
sera

Reputation: 83

How to switch window handles using Selenium and Python

If you click a link in a Windows program other than a web browser, a pop-up window appears. I want to get the url of this popup window. Pop-up windows will only open in IE.

driver = webdriver.Ie('C://Users/aaa/IEDriverServer.exe')
driver.implicitly_wait(3)
pyautogui.moveTo(1576, 660)
pyautogui.click()
time.sleep(3)
driver.switch_to_window(driver.window_handles[1])
   # error =>driver.switch_to_window(driver.window_handles[1])
   #         IndexError: list index out of range
driver.get_window_position(driver.window_handles[1])

windows = driver.window_handles
   # Commenting out the above two lines will result in only one active web 
   # browser in windows.
print(windows)

driver = webdriver.Ie('C://Users/seula/IEDriverServer.exe')
driver.implicitly_wait(3)
pyautogui.moveTo(1576, 660)
pyautogui.click()
time.sleep(3)
driver.switch_to_window(driver.window_handles[1])
   # error =>driver.switch_to_window(driver.window_handles[1])
   #         IndexError: list index out of range
driver.get_window_position(driver.window_handles[1])

windows = driver.window_handles
   # Commenting out the above two lines will result in only one active web 
   # browser in windows.
print(windows)

In this source, running IEDriver.exe opens a localhost window and pops up when you click on a link to a Windows program with pyautogui. However, if I check with driver.window_handles, only the localhost window is shown and the popup window is not recognized. How can I get the popup window url?

Upvotes: 2

Views: 7166

Answers (3)

Yun
Yun

Reputation: 1042

I usually use send_keys() instead of click() to handle pop-up window.

Try to use following code:

pyautogui.send_keys(Keys.CONTROL + Keys.ENTER)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + "2")
# window_handles[-1] refer to last window created.
driver.switch_to.window(self.driver.window_handles[-1])
url = driver.current_url
print(url)

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193348

As Simon clearly mentioned in a discussion:

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.

So you have to:

  • Induce WebDriverWait for number_of_windows_to_be(2)
  • As the order of the windows are random, you can obtain the set of window handles before the interaction is performed and compare it with the set after the action is performed.
  • You can use the following solution:

    driver = webdriver.Ie('C://Users/aaa/IEDriverServer.exe')
    driver.implicitly_wait(3)
    windows_before  = browser.current_window_handle
    pyautogui.moveTo(1576, 660)
    pyautogui.click()
    WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
    windows_after = driver.window_handles
    new_window = [x for x in windows_after if x != windows_before][0]
    driver.switch_to.window(new_window)
    

References: You can find a couple of relevant discussions in:

Upvotes: 1

frianH
frianH

Reputation: 7563

For get current url, you can use :

windows = driver.current_url
print(windows)

Upvotes: 0

Related Questions