2

I'm using Selenium Web Driver and Python. How would I loop through multiple open Chrome browser tabs and stay on each page for 30 seconds, then loop back to the first URL and go through the iteration over and over again? I'm able to open the tabs/designate the desired URL's. However I am having trouble figuring out the loop to iterate through each tab

#import from selenium driver
from selenium import webdriver
#import from use of Key actions && Action chains (commands) from selenium driver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time


#designate each URL to be opened
url_1 = 'https://www.google.com/'
url_2 = 'https://github.com/'
url_3 = 'https://www.kaggle.com/'

#designate webdriver as chrome
driver = webdriver.Chrome()

#open 1st URL in first tab
driver.get(url_1)

#wait 
driver.implicitly_wait(15)

#open new window (tab 2) and switch over to it
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])

#open 2nd URL in current tab
driver.get(url_2)

#wait 
driver.implicitly_wait(15)

#open new window (tab 3) and switch over to it
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[2])

#open 3rd URL in current tab
driver.get(url_3)

#wait 
driver.implicitly_wait(15)

thanks for any && all help

teslawannabe
  • 29
  • 1
  • 2
  • This is not a duplicate question. The duplicate question linked is about opening a tab and this question is about looping through all tabs. – vincent mathew May 19 '21 at 01:46

1 Answers1

5

Open all tabs in once, Then you should count the tabs you have. Then loop on each on:

 cnt = len(driver.window_handles)
 for x in range(cnt):
     driver.switch_to.window(driver.window_handles[x])
     sleep(30) #30sec
JoySalomon
  • 61
  • 1
  • 10
  • 1
    thank you so much!! I can't upvote since I have less than 15 reputation. But your answer is perfect, thanks!! – teslawannabe May 27 '20 at 15:53
  • `driver.window_handles` contains the window handles themselves for me (selenium 3.141.0) – Ryan Mar 08 '21 at 13:36