Alex Martian
Alex Martian

Reputation: 3812

selenium python click link open in other window, need same

myLink = myDriver.find_element_by_xpath ('....')
myLink.click()
bmyLink = WebDriverWait(myDriver, 10).until(ExConditions.presence_of_element_located((By.XPATH, '.....')))

I use selenium with Firefox driver Python 3.5. I noted that after click() another Firefox window is opened, so second search for link is done in first window I guess, not where I wanted it to be (on new opened by click page). How to make it work - open new page with click() in same window? or else?

Upvotes: 0

Views: 1434

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23825

If after first click().. open a new window, you need to switch that new window first then go to find the link as below :-

myLink = myDriver.find_element_by_xpath ('....')
myLink.click()

currentWindow = myDriver.current_window_handle 
#store current window for backup to switch back

for handle in myDriver.window_handles:
    if handle != currentWindow:
        myDriver.switch_to_window(handle)

#now you can do your stuff in new window
bmyLink = WebDriverWait(myDriver, 10).until(ExConditions.presence_of_element_located((By.XPATH, '.....')))
----

#now close the new window after doing all stuff
myDriver.close()

#after doing all stuff it new window need to switch back on main window
myDriver.switch_to_window(currentWindow)

Hope it will help you...:)

Upvotes: 2

Related Questions