Selenium-Python中的旋转选项卡

时间:2018-10-03 23:06:52

标签: python selenium selenium-webdriver

我设置了一个python脚本来打开10个标签,并在每个标签上加载一个网页。我现在需要做的是每30秒在这些标签之间旋转一次。

基本上,在全部加载之后,我只需要每30秒按Ctrl + Tab使其旋转并充当幻灯片放映。

所有提示?

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')

1 个答案:

答案 0 :(得分:3)

我将使用driver.switch_to.window来完成此任务,例如:

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