selenium框架安裝及webdriver安裝
本文介紹的是selenium安裝及webdriver安裝、小實例
1、selenium介紹
selenium是一個用於web應用程序測試的工具。
Selenium測試直接運行在瀏覽器,就向真正的用戶操作一樣。
支持的瀏覽器包括IE(7,8,9,10,11),Mazilla Firefox,Safari,Google Chrome,OperaL瀏覽器
這個工具的主要功能包括;
測試與瀏覽器的兼容性—測試你的應用程序是否能夠很好的工作在不同瀏覽器和操作系統上。
測試系統功能—穿件會回歸測試檢驗關鍵功能和用戶需求。
支持自動錄制動作和自動生成.Net,Java,Perl,Python等不同語言的測試腳本。
2、python selenium以及webdriver的安裝
pip3 install selenium
安裝成功之後,我們來下載安裝瀏覽器的驅動,selenium實現像用戶一樣的操作是需要瀏覽器驅動的,下面我們以chrome瀏覽器為例。
查看瀏覽器版本
下載對應版本的驅動,下面是下載地址(需要FQ):
https://sites.google.com/a/chromium.org/chromedrvier/downloads
或http://chromedriver.storage.googleapis.com/index.html
下載下來後我們要配置環境變量,為了偷懶不用配置,我們可以直接把下載的驅動放到python的安裝目錄下
3、 小小測試開始
Python 3.5
Selenium
Windows
Pycharm
1 from selenium import webdriver 2 3 #創建了指定的瀏覽器驅動 4 chrome = webdriver.Chrome() 5 #打開指定頁面 6 chrome.get(‘https;//www.baidu.com‘) 7 #查詢指定的元素 8 search = chrome.find_element_by_id(‘kw‘) 9 #發送值 10 search.send_keys(‘python‘)
效果如下:
4、 基於騰訊空間的selenium模擬登錄
需要真實的獲取到登錄的網頁地址,按F12查看網頁代碼,真實的qq空間登錄地址包含在iframe標簽中。
1 from selenium import webdriver 2 3 url = ‘https://xui.ptlogin2.qq.com/cgi-bin/xlogin?proxy_url=https%3A//qzs.qq.com/qzone/v6/portal/proxy.html&daid=5&&hide_title_bar=1&low_login=0&qlogin_auto_login=1&no_verifyimg=1&link_target=blank&appid=549000912&style=22&target=self&s_url=https%3A%2F%2Fqzs.qzone.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&pt_qr_app=%E6%89%8B%E6%9C%BAQQ%E7%A9%BA%E9%97%B4&pt_qr_link=http%3A//z.qzone.com/download.html&self_regurl=https%3A//qzs.qq.com/qzone/v6/reg/index.html&pt_qr_help_link=http%3A//z.qzone.com/download.html&pt_no_auth=0‘ 4 5 chrome = webdriver.Chrome() 6 #打開指定頁面 7 chrome.get(url) 8 9 #查看打開form表單的元素 10 login_form = chrome.find_element_by_id(‘switcher_plogin‘) 11 #觸發單擊事件 12 login_form.click() 13 14 #捕獲用戶名輸入框 15 username = chrome.find_element_by_id(‘u‘) 16 username.send_keys(‘1218427198‘) #輸入qq賬號 17 18 #捕獲密碼輸入框 19 password = chrome.find_element_by_id(‘p‘) 20 password.send_keys(‘xxxx‘) #xxxx為密碼 21 22 #捕獲提交按鈕 23 submit = chrome.find_element_by_id(‘login_button‘) 24 #觸發單擊事件 25 submit.click()
效果如下:
selenium框架安裝及webdriver安裝