sochirca ilie
sochirca ilie

Reputation: 11

Python switching windows with Selenium

Could you help me with an advice? I'm doing some QA test and am testing a site. During the process, one by one windows in Chrome open and I'm trying to switch between these windows in Chrome. I don't succeed to switch to the third window. Can you help please? I'm using Pycharm and Selenium framework.

Here is the part of the Code where I ask your assistance:

second_login= webdriver.Ie()
second_login.maximize_window()
second_login.get('http://google.com')
print(second_login.title)
second_login.find_element_by_id('id1').click()
tab_now = second_login.window_handles[1]
second_login.switch_to.window(tab_now)
print(second_login.title)
log = second_login.find_element_by_id('id2')
log.send_keys('admin1')
pas=second_login.find_element_by_id('id3')
pas.send_keys('pas1')
logbutton=second_login.find_element_by_class_name('LoginButton')
logbutton.click()
second_login.implicitly_wait(3)
tab_after = second_login.window_handles[-1]
second_login.switch_to.window(tab_after)
second_login.find_element_by_id('id4').click()
print(second_login.title)

Thank you.

Upvotes: 1

Views: 6342

Answers (1)

Slav Kurochkin
Slav Kurochkin

Reputation: 424

As described here, you can do it by using window_handles and switch_to_window method.

Before clicking the link first window handle as

window_before = driver.window_handles[0]

Second window handle of newly opened window as

window_after = driver.window_handles[1]

I assume the third window would be

driver.window_nandles[2]

Also take a look here, you can loop trough windows, just break on the 3rd window and do whatever you want:

for handle in driver.window_handles:
    driver.switch_to_window(handle)

Hope it helps.

Upvotes: 2

Related Questions