Ihor Harmatii
Ihor Harmatii

Reputation: 300

Selenium Python switch to new window

windows_before = driver.window_handles
driver.find_element_by_css_selector("[href='http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2']").click()
windows_after = driver.window_handles
new_window =
driver.switch_to_window(new_window)
driver.close()
driver.switch_to_window(windows_before)

Please can someone tell me how can I get the id of new window. I have windows_before = 'CDwindow-8b18d716-650e-4793-ac69-10532978004c' than I have list with id of old and new windows ['CDwindow-8b18d716-650e-4793-ac69-10532978004c', 'CDwindow-1b199b20-c50e-4301-bbea-9f0f8faa2902']

How can I get the element from my list that not match with windows_before?

Thank you

Upvotes: 1

Views: 1415

Answers (1)

double_j
double_j

Reputation: 1706

This will pick the first window in the list that is not windows_before, as long as windows_before is a string and not a list.

new_window = [x for x in windows_after if x != windows_before][0]

Upvotes: 1

Related Questions