1. webdriver簡介html
pip install selenium
2. 瀏覽器擴展下載python
#!/usr/bin/env python #coding:utf8 from selenium import webdriver url = 'https://www.baidu.com' driver = webdriver.Firefox() //建立一個firefox瀏覽器的dirver對象 driver.get(url) //訪問url driver.get_cookies() //返回當前頁的cookie driver.back() //歷史記錄後退一頁 driver.forward() //歷史記錄前進一頁 driver.refresh() //刷新當前頁 # print(driver.page_source) //返回當前的html源碼 print(driver.current_url) //返回當前的url print(driver.session_id) //session id print(driver.title) //titile driver.quit() //退出全部頁面並關閉會話 ------------------------------------------------> https://www.baidu.com/ 04b303b0-0b58-45c6-b5bb-e4aa531b4bf7 百度一下,你就知道
4. 元素對象的一些方法linux
#!/usr/bin/env python #coding:utf8 import time from selenium import webdriver url = 'https://www.python.org/' driver = webdriver.Firefox() driver.get(url) element = driver.find_element_by_id('id-search-field') element.clear() //清空輸入框的內容 element.send_keys('os') //發送數據到輸入框 # element.click() //點擊(同鼠標左鍵) # element.submit() //點擊表單提交按鈕 driver.find_element_by_id('submit').click() time.sleep(10) driver.quit()
5. 定位html中的元素對應的代碼git
實例一:在python官網搜索框搜索os模塊github
import time from selenium import webdriver url = 'https://www.python.org/' driver = webdriver.Firefox() driver.get(url) element = driver.find_element_by_id('id-search-field') element.clear() element.send_keys('os') driver.find_element_by_id('submit').click() time.sleep(10) driver.quit()
實例二:登錄京東並領取優惠券web
#!/usr/bin/env python #coding:utf8 import time import string import random from selenium import webdriver def getTime(): return time.sleep(int(random.choice(string.digits))) url = 'https://www.jd.com/' driver = webdriver.Firefox() driver.get(url) getTime() driver.find_element_by_link_text('你好,請登陸').click() getTime() driver.find_element_by_xpath('//a[@clstag="pageclick|keycount|201607144|2"]').click() element = driver.find_element_by_id('loginname') getTime() element.clear() element.send_keys('xxxxxx') element1 = driver.find_element_by_id('nloginpwd') getTime() element1.clear() element1.send_keys('xxxxxxx') getTime() driver.find_element_by_id('loginsubmit').click() driver.get('https://plus.jd.com/index') driver.find_element_by_xpath('//a[@clstag="plus2017|keycount|ZSQ|coupon_get1_46793030"]').click()