我的筆記,摘抄自蟲師python selenum,僅供我的參考css
一、安裝:html
pip install selenium
下載webdriver # webdriver 下載並放置在python主目錄
二、基本使用java
from selenium import webdriver browser = webdriver.Firefox() browser = webdriver.Chrome() browser.set_window_size(480, 800) #參數數字爲像素點 browser.maximize_window() #將瀏覽器最大化顯示 browser.get(url) browser.back() browser.forward() browser.close() # browser.quit()
三、元素定位python
# 單個元素 browser.find_element_by_id() browser.find_element_by_name() browser.find_element_by_tag_name() browser.find_element_by_class_name() browser.find_element_by_css_selector() browser.find_element_by_xpath() # 多個元素 browser.find_elements_by_[...]() #層級定位 #點擊Link1連接(彈出下拉列表) dr.find_element_by_link_text('Link1').click() #找到id 爲dropdown1的父元素 WebDriverWait(dr, 10).until(lambda the_driver: the_driver.find_element_by_id('dropdown1').is_displayed()) #在父親元件下找到link爲Action的子元素 menu = dr.find_element_by_id('dropdown1').find_element_by_link_text('Action') #鼠標定位到子元素上 webdriver.ActionChains(dr).move_to_element(menu).perform() # 鼠標移到懸停元素上 ActionChains(browser).move_to_element(ele).perform()
四、 操做對象、WebElement 經常使用方法,小結git
操做對象:github
WebElement 另外一些經常使用方法:web
五、定位framechrome
多層框架或窗口的定位:瀏覽器
智能等待:cookie
調用js方法
在當前窗口/框架 同步執行javaScript
腳本:JavaScript的執行。
*參數:適用任何JavaScript腳本。
使用:
driver.execute_script(‘document.title’)
七、selenium 對下拉框信息的定位,以及對警告提示框的處理
本節重點
js="var q=document.getElementById('id').scrollTop=10000" driver.execute_script(js) js="var q=document.documentElement.scrollTop=10000" driver.execute_script(js)
九、模擬鍵盤按鍵操做
from selenium.webdriver.common.keys import Keys #須要引入keys包 driver.find_element_by_id("user_name").send_keys(Keys.TAB) driver.find_element_by_id("user_pwd").send_keys(Keys.ENTER) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a') driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'c') driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v') send_keys(u"輸入中文")
module:selenium.webdriver.common.keys • class Keys() – NULL = u’ue000’ – CANCEL = u’ue001’ # ^break – HELP = u’ue002’ – BACK_SPACE = u’ue003’ – TAB = u’ue004’ – CLEAR = u’ue005’ – RETURN = u’ue006’ – ENTER = u’ue007’ – SHIFT = u’ue008’ – LEFT_SHIFT = u’ue008’ # alias – CONTROL = u’ue009’ – LEFT_CONTROL = u’ue009’ # alias – ALT = u’ue00a’ – LEFT_ALT = u’ue00a’ # alias – PAUSE = u’ue00b’ – ESCAPE = u’ue00c’ – SPACE = u’ue00d’ – PAGE_UP = u’ue00e’ – PAGE_DOWN = u’ue00f’ – END = u’ue010’ – HOME = u’ue011’ – LEFT = u’ue012’ – ARROW_LEFT = u’ue012’ # alias – UP = u’ue013’ – ARROW_UP = u’ue013’ # alias – RIGHT = u’ue014’ – ARROW_RIGHT = u’ue014’ # alias – DOWN = u’ue015’ – ARROW_DOWN = u’ue015’ # alias – INSERT = u’ue016’ – DELETE = u’ue017’ – SEMICOLON = u’ue018’ – EQUALS = u’ue019’ – NUMPAD0 = u’ue01a’ # numbe pad keys – NUMPAD1 = u’ue01b’ – NUMPAD2 = u’ue01c’ – NUMPAD3 = u’ue01d’ – NUMPAD4 = u’ue01e’ – NUMPAD5 = u’ue01f’ – NUMPAD6 = u’ue020’ – NUMPAD7 = u’ue021’ – NUMPAD8 = u’ue022’ – NUMPAD9 = u’ue023’ – MULTIPLY = u’ue024’ – ADD = u’ue025’ – SEPARATOR = u’ue026’ – SUBTRACT = u’ue027’ – DECIMAL = u’ue028’ – DIVIDE = u’ue029’ – F1 = u’ue031’ # function keys – F2 = u’ue032’ – F3 = u’ue033’ – F4 = u’ue034’ – F5 = u’ue035’ – F6 = u’ue036’ – F7 = u’ue037’ – F8 = u’ue038’ – F9 = u’ue039’ – F10 = u’ue03a’ – F11 = u’ue03b’ – F12 = u’ue03c’ – META = u’ue03d’ – COMMAND = u’ue03d’
ActionChains 類
from selenium.webdriver.common.action_chains import ActionChains #定位到要雙擊的元素 qqq =driver.find_element_by_xpath("xxx") #對定位到的元素執行鼠標雙擊操做 ActionChains(driver).double_click(qqq).perform() #定位元素的原位置 element = driver.find_element_by_name("source") #定位元素要移動到的目標位置 target = driver.find_element_by_name("target") #執行元素的移動操做 ActionChains(driver).drag_and_drop(element, target).perform()