Selenium - Python 中的旋转标签

我有一个 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')


动漫人物
浏览 165回答 1
1回答

呼啦一阵风

我将driver.switch_to.window用于此任务,例如:while True:    Windows = driver.window_handles    for window in Windows:        driver.switch_to.window(window)        time.sleep(30)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python