一、webdriver基本使用命令
from selenium import webdriver # 導入webdriver模塊 >>> chrome_obj = webdriver.Chrome() # 打開Google瀏覽器 >>> chrome_obj.get("https://www.baidu.com") # 打開 網址 >>> chrome_obj.get(r"C:\desktop\text.html") # 打開本地 html頁面 >>> chrome_obj.title # 獲取打開網址 的名字 >>> chrome_obj.current_url # 獲取打開網址的 url
>>> chrome_obj.close() #關閉瀏覽器窗口
二、標簽導航
普通 定位標簽
# 查找標簽 >>> label = chrome_obj.find_element_by_id("kw") >>> label = chrome_obj.find_element_by_name("wd") >>> label = chrome_obj.find_element_by_class_name("s_ipt") >>> label = chrome_obj.find_element_by_tag_name("imput") >>> label = chrome_obj.find_element_by_link_text("a標簽中的內容 准確定位") >>> label = chrome_obj.find_element_by_partial_link_text("a標簽中的內容 模糊定位 ") >>> label = chrome_obj.find_element_by_xpath(“放入 copy 標簽中的常css路徑”) >>> label = chrome_obj.find_element_by_css_selector(“input=[id='id_name'/name='name_name'/……/]")
標簽導航 xpath 標簽定位復雜的情況下 考慮使用xpath
XPath即為XML路徑語言,它是一種用來確定XML(標准通用標記語言的子集)文檔中某部分位置的語言。XPath基於XML的樹狀結構,有不同類型的節點,包括元素節點,屬性節點和文本節點,提供在數據結構樹中找尋節點的能力。

# 絕對路徑 >>> label = chrome_obj.find_element_by_xpath("html/boday/p/input") # 絕對路徑 導航 >>> label = chrome_obj.find_element_by_xpath("html/boday/p/input[1]") # 絕對路徑導航,多個input框,確定第一個input框 #相對路徑 >>> label = chrome_obj.find_element_by_xpath("//input") # 相對路徑導航 表示 整個文檔當中的 input標簽 默認為第一個 * 第一個“//” 表示 在整個文檔中 >>> label = chrome_obj.find_element_by_xpath("//input[2]") # 指定頁面中的第二個 input框 沒有就報錯 # 父節點下找子節點 >>> label = chrome_obj.find_element_by_xpath("//form//input") # // 父節點//子節點 * 返回子節點 input >>> label = chrome_obj.find_element_by_xpath("//form//input[2]") # // 父節點//子節點 [2] * 指定 父節點下的 第二個 input子節點 # 通過子節點 定位父節點 >>> label = chrome_obj.find_element_by_xpath("//form//input/..") # 返回input的父節點 form 標簽 >>> label = chrome_obj.find_element_by_xpath("//form//input/.") # 當前節點 # 通過屬性查找節點 >>> label = chrome_obj.find_element_by_xpath("//input[@id]") # 相對路徑導航 找到所有的 input標簽 其中有 id屬性的標簽 >>> label = chrome_obj.find_element_by_xpath("//input[@id='1']") # 屬性查找 在所有的input標簽中 找到 具有 id=1 的input標簽 >>> label = chrome_obj.find_element_by_xpath("//input[@name='xiahua']") # 標簽統計 countains >>> label = chrome_obj.find_element_by_xpath("//*[countains(input)=1]") # //* 表示 整個文檔中 的所有標簽,[count(input)=1] 表示 父標簽下只有 一個input子標簽 的 input標簽 >>>label = chrome_obj.find_element_by_xpath("//*[countains(input)=2]") # //* 表示 整個文檔中 的所有標簽,[count(input)=1] 表示 父標簽下有 兩個input子標簽 的 input標簽 # local-name 模糊查找 >>> label = chrome_obj.find_element_by _xpath("//*[local-name()='input']") # 查找當前文檔中 的所有input標簽 默認返回第一個 >>> label = chrome_obj.find_element_by _xpath("//*input") # 查找當前文檔中 的所有input標簽 默認返回第一個 >>> label = chrome_obj.find_element_by _xpath("//*[local-name(),'i']") # 查找當前文檔中 標簽名字中 包含字母 i的標簽,比如 input title >>> label = chrome_obj.find_element_by _xpath("//*[local-name(),'i']") # 查找當前文檔中 的所有input標簽 默認返回第一個 >>> label = chrome_obj.find_element_by _xpath("//*[countains(local-name(),'i')] [last()]) # 查找當前文檔中 所有包含 字母“i”的 標簽 的子標簽 的 最后一個元素 (有點懵逼) >>> label = chrome_obj.find_element_by _xpath("//*[strint-length(local-name()=3)] [last()]) # 查找當前文檔中 所有 標簽字符個數為5的標簽,並且制定返回 最后一個標簽。 title input(5個str)
三、 模擬用戶操作
>>> label.get_attribute("type") # 顯示標簽的type屬性 name type id placeholder >>> label.tag_name() #獲取標簽名字 input p form …… >>> label.size >>> label.id >>> chrome_obj.maximize_window()# 窗口最大化
>>> #模擬鼠標懸浮
>>> label.click() # 模擬a標簽 點擊事件
>>> label.send_keys("模擬搜索內容") # 模擬input框 輸入內容
>>> label.clear() # 清除input標簽中 輸入的內容
>>> chrome_obj.back() # 模擬瀏覽器 返回上一個瀏覽頁面
1、模擬鼠標操作
from selenium.webdriver.common.action_chains import ActionChains #導入模塊 >>> label = chrome_obj.find_element_by_link_text("點我 懸浮 顯示其他 a標簽") >>> ActionChains(chrome_obj).move_to_element(label).perform() # 模擬用戶懸浮
""" ActionChains(chrome_obj) 用於生成模擬用戶行為 ;
perform() 執行存儲行為 """
>>> label_bel = chrome_obj.find_element_by_link_text("我是 a標簽,點我頁面跳轉")
>>> label_bel.click() # 模擬用戶點擊
其他鼠標操作
label.countext_lick() # 右擊 label.double_click() # 雙擊 label.drag_and_drop() # 拖動 label.move_to_element # 懸浮 label.click_and_hold # 按鼠標左鍵一直不動
2、模擬鍵盤操作
from selenium.webdriver.common.keys import Keys # 引入模塊 >>> label.send_keys("input輸入的內容") >>> label.send_keys(Keys.BACK_SPANCE) # 退格鍵 >>>label.send_keys(Keys.CONTRL,'a') # 全選 >>>label.send_keys(Keys.CONTRL,'v') # 粘貼 >>>label.send_keys(Keys.CONTRL,'c') # 復制 >>>label.send_keys(Keys.CONTRL,'x‘’) # 剪切 >>>label.send_keys(Keys.ENTER) # 回車
四、處理對話框
python腳本實現自動登錄

from selenium import webdriver import time def automatic_login(name,pwd,url): chrome = webdriver.Chrome() chrome.get(url) time.sleep(2) chrome.maximize_window() time.sleep(5) chrome.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div").click() chrome.find_element_by_link_text("登錄").click() time.sleep(5) name_label = chrome.find_element_by_id("id_account_l") name_label.clear() name_label.send_keys(name) pwd_label = chrome.find_element_by_id("id_password_l") pwd_label.clear() pwd_label.send_keys(pwd) time.sleep(5) login_label = chrome.find_element_by_id("login_btn") login_label.click() time.sleep(15) chrome.close() if __name__ == "__main__": name = "helloyiwantong@163.com" pwd = "helloyiwantong@1234" url = "http://www.maiziedu.com/" automatic_login(name,pwd,url)
五、控制多窗口

>>> frome selenium import webdrive >>> chrome = webdrive.Chrome() >>> chrome.get("https://www.baidu.come") >>> chrome.find_element_by_id("kw").send_keys("紅花") >>> chrome.find_element_by_id("su").click() # 打開百度搜索的第一個窗口 >>> chrome.find_element_by_partial_link_text("百度百科").click() # 打開第二個窗口 >>> chrome.find_element_by_partial_link_text("中葯").click() # 打開第三個窗口 >>> chrome.window_handles # 查看當前 打開窗口 ['CDwindow-D41F1F3BF5038E36E91EA7F7E7E9770D', 'CDwindow-F2D1553323BDC39BE99DBF280804FCCC', 'CDwindow-B41B29B8A7CB49BF191E46FF936E6A52',] >>> chrome.switch_to_window(chrome.window_handles[1]) # 使用索引切換到第二個窗 >>> chrome.current_url() # 查看當前url
六、模擬用戶自動登錄

from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait def wait_response_time(chrome,waittime,func): # 返回 func執行結果 return WebDriverWait(chrome,waittime).until(func) def automatic_login(name,pwd,url): chrome = webdriver.Chrome() chrome.get(url) time.sleep(2) chrome.maximize_window() # time.sleep(2) # chrome.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div").click() ############## 第二種方法 設置時間延遲 # login_btn_lable = wait_response_time(chrome,5, # lambda chrome: chrome.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div")) # login_btn_lable.click() ## 利用函數設置等待響應時間 chrome.find_element_by_link_text("登錄").click() time.sleep(1) name_label = chrome.find_element_by_id("id_account_l") name_label.send_keys(" ") # 防止發送不成功 name_label.clear() name_label.send_keys(name) pwd_label = chrome.find_element_by_id("id_password_l") pwd_label.clear() pwd_label.send_keys(pwd) time.sleep(5) login_label = chrome.find_element_by_id("login_btn") login_label.click() error_id="login-form-tips" error_message=chrome.find_element_by_id(error_id) err=error_message.text print(error_message,type(error_message)) print(err) # time.sleep(10) # chrome.close() if __name__ == "__main__": name = "helloyiwantong@163.com" pwd = "helloyiwantong@134" url = "http://www.maiziedu.com/" automatic_login(name,pwd,url)
七、模擬用戶自動登錄 封裝接口

from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait def open_browse(): """ open browser obj :return: """ browse_obj = webdriver.Chrome() return browse_obj def click_url_and_clicl_loginbtn(browse,url): """ open url and click "登錄" btn :param browse: :param url: :return: """ browse.get(url) browse.maximize_window() loginbtn_lable=browse.find_element_by_link_text("登錄") loginbtn_lable.click() time.sleep(1) def get_element_label(browse,element_id_dict): """ get element lable :param browse: :param element_id_dict: :return: """ user_label = browse.find_element_by_id(element_id_dict["name"]) pwd_label = browse.find_element_by_id(element_id_dict.get("pwd")) login_label = browse.find_element_by_id(element_id_dict.get("login")) return (user_label,pwd_label,login_label) def send_key_s(lable_tuple,userinfo_dict,userinfo_list): """ send userinfo and logian :param lable_tuple: :param userinfo_dict: :param userinfo_list: :return: """ i=0 if i<=1: for key in userinfo_list: # lable_tuple[i].send_keys(" ") # lable_tuple[i].click() lable_tuple[i].send_keys(userinfo_dict.get(key)) i+=1 time.sleep(1) lable_tuple[2].click() ### 封裝數據 url = "http://www.maiziedu.com/" id_dict = { "name":"id_account_l", "pwd" : "id_password_l", "login":"login_btn", } userinfo_dict={ "name" : "helloyiwantong@163.com", "pwd" : "helloyiwantong@1234", "url" : "http://www.maiziedu.com/", } # 函數使用 userinfo_list =["name","pwd"] chrome = open_browse() click_url_and_clicl_loginbtn(chrome,url) lable_tuple = get_element_label(chrome,id_dict) send_key_s(lable_tuple,userinfo_dict,userinfo_list)