selenium+python自动化测试(二)对浏览器的简单操作
1.最大化
maximize_window
1 # coding = utf-8 2 3 from selenium import webdriver 4 chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver" 5 browser = webdriver.Chrome(chromedriver) 6 7 browser.get('http:\\www.baidu.com') 8 browser.maximize_window() 9 browser.find_element_by_id('kw').send_keys('selenium') 10 browser.find_element_by_id('su').click() 11 12 browser.quit()
发现运行时候报错了:
selenium.common.exceptions.WebDriverException: Message: disconnected: unable to connect to renderer
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64)
就是chrome和chromedriver版本不对应。关键词检索了下,发现的原因。然后下载了个更新的版本
http://chromedriver.storage.googleapis.com/index.html?path=2.34/
替换下就运行OK了
2.前进和后退
1 rom selenium import webdriver 2 import time 3 chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver" 4 browser = webdriver.Chrome(chromedriver) 5 first_url = 'http:\\www.baidu.com' 6 second_url = 'http:\\news.baidu.com' 7 browser.get(first_url) 8 browser.get(second_url) 9 browser.back() 10 browser.maximize_window() 11 browser.forward() 12 browser.back() 13 time.sleep(5) 14 browser.set_window_size(480, 800) 15 browser.quit()
参考网页:
1. http://blog.csdn.net/huilan_same/article/details/51896672