目录

Step 1:安装浏览器driver

浏览器 Driver
Chrome chromedriver
Firefox geckodriver

Step 2:安装selenium

$ pip install selenium

STep 3: 使用selenium

例子:打开两个tab,并在tab之间定期切换:

import time

from selenium import webdriver
from selenium.webdriver import ActionChains

obj = webdriver.Chrome()
obj.maximize_window()

obj.get("http://www.qq.com/")
obj.implicitly_wait(15)
e1 = obj.find_element_by_xpath('//*[@id="navBeta"]/div[1]/div[1]/a[1]')
ActionChains(obj).click(e1).perform()
obj.implicitly_wait(15)
tabs = obj.window_handles
print(tabs)
while True:
    for i in tabs:
        obj.switch_to.window(i)
        time.sleep(2)

参考