Python – Selenium – Rotate labels in Python

Selenium – Rotate labels in Python… here is a solution to the problem.

Selenium – Rotate labels in Python

I have a python script set up to open 10 tabs and load a web page on each tab. What I need to do now is have it rotate between these tabs every 30 seconds.

Basically, after it’s all loaded, I just need to press Ctrl+Tab every 30 seconds so it spins and serves as a slide show.

All tips?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
usernameStr = 'username'
passwordStr = 'password'

options = Options()
options.add_argument('--kiosk')
options.add_argument('disable-infobars')
driver = webdriver. Chrome(chrome_options=options, executable_path=r'C:\Users\username\Desktop\chromedriver.exe')
driver.get('http://website.com')
# fill in username and hit the next button
username = driver.find_element_by_id('username')
username.send_keys(usernameStr)
password = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'password')))
password.send_keys(passwordStr)
nextButton = driver.find_element_by_class_name('emp-submit')
nextButton.click()

#second tab
driver.execute_script("window.open('about:blank', 'tab2');" )
driver.switch_to.window("tab2")
driver.get('http://website.com')

#third tab
driver.execute_script("window.open('about:blank', 'tab3');" )
driver.switch_to.window("tab3")
driver.get('http://website.com')

#fourth tab
driver.execute_script("window.open('about:blank', 'tab4');" )
driver.switch_to.window("tab4")
driver.get('http://website.com')

Solution

I’ll use driver.switch_to.window for this task, for example:

while True:
    Windows = driver.window_handles
    for window in Windows:
        driver.switch_to.window(window)
        time.sleep(30)

Related Problems and Solutions