0

I'm trying to open site in new tab, code:

from selenium import webdriver
import time
driver = webdriver.Chrome('/home/metulburr/chromedriver')
# List of opened tabs
print('before sleeping', window_handles)
# Open a new window
# This does not change focus to the new window for the 
driver.driver.execute_script("window.open('');")
# Switch to the last window
driver.switch_to.window(driver.window_handles[-1])
print('after sleeping', window_handles)

But I'm failing because not all tabs are loaded

OUTPUT:

# Not all opened tabs
`before sleeping: ['CDwindow-112051A6FC23C19FA18D321731625FC9']`

#  All opened tabs
`after sleeping: ['CDwindow-112051A6FC23C19FA18D321731625FC9', 'CDwindow-532838AFFB2AE8E9EDA90EE64EAF5B35', 'CDwindow-CC54E7485C7B05C3D4B2FB689223BE17', 'CDwindow-28A3CFCF6F2958F3D4467E524BC4FB8E', 'CDwindow-E407D73A0C9F6217C05DE8D4E39BFD4F', 'CDwindow-4AD3703CB6EBB1422CB1372542CF539D', 'CDwindow-CE33E7029F395EA85870BDD5483FFF73', 'CDwindow-0CD0D27B0C3C883A38A741EF599EB349', 'CDwindow-D29CB358CDF127688C3ED6EDCE62348F']`

How I can wait while browser will load his resources after a startup (opened tabs particulary) without time.sleep() ?

salius
  • 113
  • 7
  • Your code works fine with minor fixes. Output I got after executing your code - `before sleeping ['CDwindow-6E105BAFB064152C54AA9E3C38ECE238'] after sleeping ['CDwindow-6E105BAFB064152C54AA9E3C38ECE238', 'CDwindow-4636328788BF13C1208911E670BFAD15']` Is something else that you forgot to mention or add? – Dev Jul 29 '20 at 11:21

1 Answers1

1

You probably fix it with a loop that iterate all windows. For each window open a new tab, switch to tab and use driver.get('url').

At the end of the loop you will be at the last tab. And no need of time.sleep()

  • 1
    You are right, I thought about such solution, but anyway this is not a perfect solution. I hope that exist a API from Selenium to manage tabs loading – salius Jul 29 '20 at 11:34
  • I don't know, if it does exists it should be here: https://www.selenium.dev/selenium/docs/api/py/api.html – Rubén Paredes Jul 29 '20 at 11:47