0

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

  • 1
    this should get you going: https://stackoverflow.com/a/28716311/6770704 – Ian Jun 25 '20 at 11:09
  • 1
    Does [this](https://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium) answer your question? – AnsFourtyTwo Jun 25 '20 at 11:09
  • 1
    Does this answer your question? [How do I switch to the active tab in Selenium?](https://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium) – AnsFourtyTwo Jun 25 '20 at 11:10

2 Answers2

1

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)
VenuBhaskar
  • 84
  • 1
  • 8
0

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.