Rahul Singh
Rahul Singh

Reputation: 3

Switching focus to new tab python selenium web driver

I have a python selenium code to test a website. Now once the driver click on a link in the 1st tab, it opens it up in a new tab, but the focus stays on the previous tab, and I am not able to reference any elements in the new tab.

How can change the focus to the new?

Also both the tabs are open in the same browser and I am using Chrome browser

Upvotes: 0

Views: 2994

Answers (2)

Okechukwu Malcolm
Okechukwu Malcolm

Reputation: 37

You can loop through the window handles, record the counts of window handles and switch with count - 1. Check below.

n = 0
for handle in driver.window_handles:
       n+=1
driver.switch_to_window(n-1)

If you encounter any difficulty, please paste your code for further elucidation.

Upvotes: 0

VenuBhaskar
VenuBhaskar

Reputation: 54

name each window if you are working with a few number of windows. using this chunk you can name and switch between tabs

window1= driver.window_handles[1]
driver.switch_to_window(window1)

Upvotes: 1

Related Questions