1、安装selenium
1.1、Python 安装 selenium 模块
1.2、下载驱动
选择和自己chrom版本相对应的驱动到本地,下载地址:http://npm.taobao.org/mirrors/chromedriver/
2、Python 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| from selenium import webdriver import time import json
driver = webdriver.Chrome('D:\home\chrom\chromedriver.exe') driver.maximize_window()
driver.get('https://baidu.com') time.sleep(5)
driver.get(driver.current_url) driver.refresh()
el = driver.find_element_by_xpath("//input[@type='text']")
el.send_keys('***********')
el = driver.find_element_by_id("submit-login")
el.click()
el = driver.find_element_by_class_name("shadow-close") el.click()
diccookie=driver.get_cookies() fw=open('cookie.txt','w') json.dump(diccookie,fw) fw.close()
fr=open('cookie.txt','r') cookielist=json.load(fr) fr.close() for cookie in cookielist: driver.add_cookie(cookie)
driver.get_screenshot_as_file('foo.png')
js_top = "var q=document.documentElement.scrollTop=0"
js_bottom = "var q=document.documentElement.scrollTop=10000" js_bottom2 = "window.scrollTo(0,document.body.scrollHeight)"
driver.execute_script(js_top)
driver.quit()
|