Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Rotating Tabs in Python

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

Basically, after it's all loaded I just need it to Ctrl+Tab every 30 seconds so it rotates and acts 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')
like image 955
sic_null Avatar asked Jun 14 '26 20:06

sic_null


1 Answers

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

while True:
    Windows = driver.window_handles
    for window in Windows:
        driver.switch_to.window(window)
        time.sleep(30)
like image 162
PixelEinstein Avatar answered Jun 16 '26 11:06

PixelEinstein