1 from selenium import webdriver 2 from time import sleep 3 import random 4 class testCase(object): 5 def __init__(self): 6 url = 'http://www.baidu.com' 7 self.driver = webdriver.Chrome() 8 self.driver.maximize_window() 9 self.driver.get(url) 10 11 def test_prop(self): 12 print('浏览器名称:', self.driver.name) # 浏览器名称 13 print('url:', self.driver.current_url) # url 14 print('title', self.driver.title) # title 15 print('句柄', self.driver.window_handles) # 句柄 16 print('源码:', self.driver.page_source) # 源码 17 18 def test_method(self): 19 self.driver.find_element_by_id('kw').send_keys('selenium') 20 self.driver.find_element_by_id('su').click() 21 sleep(3) 22 self.driver.back() 23 sleep(3) 24 self.driver.forward() 25 sleep(3) 26 self.driver.quit() 27 28 # self.driver.quit() # 关闭浏览器 29 # self.driver.close() # 关闭当前tab 30 31 def test_windows(self): 32 self.driver.find_element_by_link_text('新闻').click() 33 windows = self.driver.window_handles 34 35 while True: 36 for w in windows: 37 self.driver.switch_to_window(w) 38 random_number = random.uniform(0, 2) 39 print(random_number) 40 sleep(random_number) 41 42 if __name__ == '__main__': 43 case = testCase() 44 # case.test_prop() 45 # case.test_method() 46 case.test_windows() 47 pass