selenium简单代码入门
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 import os,sys,string 5 import time 6 import unittest 7 from selenium import webdriver 8 from selenium.webdriver.common.by import By 9 10 #unittest是python下的单元测试模块 11 class WeiboDate(unittest.TestCase): 12 13 #初始化,并使用Firefox浏览器驱动 14 def setUp(self): 15 self.driver = webdriver.Firefox() 16 17 def test_WeiboSearch(self): 18 driver = self.driver 19 driver.get("http://data.weibo.com/index/") 20 #wait 21 driver.implicitly_wait(10) 22 driver.find_element(By.XPATH,"//div[@id='pl_index_searchMain']//input").send_keys("apple watch") 23 driver.find_element_by_css_selector("a.index-search").click() 24 time.sleep(3) 25 #截图 26 driver.get_screenshot_as_file("/opt/selenium/script/jietu.png") 27 #最大化浏览器 28 driver.maximize_window() 29 driver.forward() 30 #cookie 31 cookie = driver.get_cookies() 32 file=open('cookie','w+').writelines("cookie") 33 34 def tearDown(self): 35 self.driver.close() 36 37 if __name__ == "__main__": 38 unittest.main() 39 40 41