- 安裝selenium 類
- 安裝chromedriver
- 定位騰訊企業郵箱的元素
http://npm.taobao.org/mirrors...
# -*- coding:utf8 -*- from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver_path = 'C:\\Users\\silence_sean\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe' driver = webdriver.Chrome(executable_path=driver_path) driver.get('https://exmail.qq.com/login') driver.find_element_by_class_name('js_show_pwd_panel').click() driver.find_element_by_id('inputuin').send_keys('xxxxx.com') driver.find_element_by_id('pp').send_keys('xxxxx') driver.find_element_by_id('btlogin').click()
經常使用方法函數 ====== 1. 加載瀏覽器驅動: webdriver.Chrome() 2. 打開頁面:get() 3. 關閉瀏覽器:quit() 4. 最大化窗口: maximize\_window() 5. 設置窗口參數:set\_window\_size(600,800) 6. 後退到前一頁: back() 7. 前進到後一頁: forward() 8. 刷新頁面: refresh() 9. 元素定位: * id定位:find\_element\_by\_id() * name定位:find\_element\_by\_name() * class定位:find\_element\_by\_class() * tag定位:find\_element\_by\_tag\_name() * link定位:find\_element\_by\_link\_text() * partial link 定位: find\_element\_by\_partial\_link\_text() * CSS定位:find\_element\_by\_css\_selector() * Xpath定位: * 絕對路徑:find\_element\_by\_xpath("/html/body/div\[x\]/div\[x\]/div/div/dl\[x\]/dt/a") * 元素屬性:find\_element\_by\_xpath("//unput\[@id=‘kw’\]") * 層級與屬性結合:find\_element\_by\_xpath("//form\[@id=‘loginForm’\]/ul/input\[1\]") * 邏輯運算符:find\_element\_by\_xpath("//input\[@id=‘kw’ and@class=‘s\_ipt’\]") 10. 清除文本:clear() 11. 模擬按鍵輸入:send\_keys(\*value)11.模擬按鍵輸入:send\_keys(\*value) 12. 單擊元素:click() 13. 提交表單(至關於"回車"):submit() 14. 鼠標事件:
#coding:utf-8 from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).***opration(opra)*** .perform() elemengt = driver.find_element_by_xpath("xpath") ActionChains(driver). double_click(DoubleClick) .perform()#雙擊 ActionChains(driver). context_click(RightClick) .perform()#右擊 ActionChains(driver). drag_and_drop(Start, End) .perform()#拖放 ActionChains(driver). move_to_element(Above) .perform()#懸停 ActionChains(driver). click_and_hold(leftclick) .perform()#按下 ```
元素等待:css
顯示等待html
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC base_url = "http://www.baidu.com" driver = webdriver.Firefox() driver.implicitly_wait(5) '''隱式等待和顯示等待都存在時,超時時間取兩者中較大的''' locator = (By.ID,'kw') driver.get(base_url) WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道")) '''判斷title,返回布爾值''' WebDriverWait(driver,10).until(EC.title_contains(u"百度一下")) '''判斷title,返回布爾值''' WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw'))) '''判斷某個元素是否被加到了dom樹裏,並不表明該元素必定可見,若是定位到就返回WebElement''' WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su'))) '''判斷某個元素是否被添加到了dom裏而且可見,可見表明元素可顯示且寬和高都大於0''' WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw'))) '''判斷元素是否可見,若是可見就返回這個元素''' WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav'))) '''判斷是否至少有1個元素存在於dom樹中,若是定位到就返回列表''' WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav'))) '''判斷是否至少有一個元素在頁面中可見,若是定位到就返回列表''' WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u'設置')) '''判斷指定的元素中是否包含了預期的字符串,返回布爾值''' WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下')) '''判斷指定元素的屬性值中是否包含了預期的字符串,返回布爾值''' #WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator)) '''判斷該frame是否能夠switch進去,若是能夠的話,返回True而且switch進去,不然返回False''' #注意這裏並無一個frame能夠切換進去 WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap'))) '''判斷某個元素在是否存在於dom或不可見,若是可見返回False,不可見返回這個元素''' #注意#swfEveryCookieWrap在此頁面中是一個隱藏的元素 WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click() '''判斷某個元素中是否可見而且是enable的,表明可點擊''' driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click() #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click() #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su'))) '''等待某個元素從dom樹中移除''' #這裏沒有找到合適的例子 WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"))) '''判斷某個元素是否被選中了,通常用在下拉列表''' WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True)) '''判斷某個元素的選中狀態是否符合預期''' WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True)) '''判斷某個元素的選中狀態是否符合預期''' driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click() instance = WebDriverWait(driver,10).until(EC.alert_is_present()) '''判斷頁面上是否存在alert,若是有就切換到alert並返回alert的內容''' print(instance.text) instance.accept() driver.close()
隱式等待web
from selenium.common.exceptions import NoSuchElementException drive.implicitly_wait(10)
得到title並打印chrome
#coding:utf-8 from selenium import webdriver title = driver.title print(title) if title == u"百度一下,你就知道":#比較title print("title yes!") else: print("title no!") url = driver.current_url#得到當前URL並打印 print(url)
滾動條設置(2種方式):npm
# 使用scrollTop滑動到底部 js = "var action=document.documentElement.scrollTop=10000" driver.execute_script(js) # 使用scrollTo設置位置 driver.set_window_size(600, 600) js = "window.scrollTo(100,450);" driver.execute_script(js)