如何在python中使用selenium的示例
最近基于selenium寫(xiě)了一個(gè)python小工具,記錄下學(xué)習(xí)記錄,自己運(yùn)行的環(huán)境是Ubuntu 14.04.4, Python 2.7,Chromium 49.0,ChromeDriver 2.16
selenium簡(jiǎn)介
selenium提供了一個(gè)通用的接口,可模擬用戶來(lái)操作瀏覽器,比如用于自動(dòng)化測(cè)試等.
selenium的核心是WebDriver,它提供了一組接口,這些接口能夠操作各種跨平臺(tái)的瀏覽器.各大瀏覽器廠商.
各大瀏覽器廠商也支持Selenium,將其作為瀏覽器的一部分.
selenium工具集提供了WebDriver,Selenium IDE,Selenium-Grid等
Selenium 1.0 + WebDriver = Selenium 2.0
Selenium WebDriver是Selenium Remote Control(Selenium-RC)的繼承者.
- WebDriver提供了更簡(jiǎn)單和簡(jiǎn)潔的接口,克服了Selenium-RC API一些限制.
- 相比Selenium 1.0,WebDriver是面向?qū)ο笫降姆?wù).
- WebDriver驅(qū)動(dòng)瀏覽器更有效率,提供了比Selenium 1.0更多的功能
- Selenium RC只能在單機(jī)上運(yùn)行,WebDriver則提供了遠(yuǎn)程操作的功能
selenium基本使用
selenium運(yùn)行需要什么
主要包括三部分:selenium selenium,瀏覽器driver,瀏覽器selenium selenium是一組通用的接口,而不同的瀏覽器提供其自身的driver(大部分是官方的),瀏覽器則被模擬控制操作的終端.
安裝
pip install selenium --upgrade apt-get install chromium-browser wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconf LONG_BIT`.zip unzip chromedriver_linux32.zip cp chromedriver /usr/local/share chmod +x /usr/local/share/chromedriver ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver ln -s /usr/bin/chromedriver /usr/local/share/chromedriver
簡(jiǎn)單的使用
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('http://mail.sina.net');
print(driver.title)
API使用
可參考/usr/local/lib/python2.7/dist-packages/selenium
Chrome WebDriver
selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)
ChromeOptions
可以通過(guò)ChromeDriver session配置ChromeDriver session ChromeDriverconvenient methods for setting ChromeDriver-specific capabilities
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-logging')
chrome_options.add_experimental_option('prefs', {'download.default_directory':
'/tmp'})
chrome_options.binary_location='/usr/bin/chromium-browser'
driver = webdriver.Chrome(chrome_options=chrome_options)
直接使用DesiredCapabilities
ChromeOptions是構(gòu)建在DesiredCapabilities之上的,為了使用DesiredCapabilities,必須知道capability的Key/value對(duì).
chrome_options = Options()
capabilities={}
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
capabilities.update(chrome_options.to_capabilities())
driver = webdriver.Chrome(desired_capabilities=capabilities)
chromedriver運(yùn)行方式
The ChromeDriver class不斷的創(chuàng)建實(shí)例,會(huì)浪費(fèi)很多的時(shí)間,可以通過(guò)兩個(gè)方式解決.
使用ChromeDriverService
import selenium.webdriver.chrome.service as service
service = service.Service('/usr/bin/chromedrive')
service.start()
capabilities = { }
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://mail.sina.net');
print(driver.title)
開(kāi)啟單獨(dú)的ChromeDriver服務(wù)
./chromedriver
driver = webdriver.Remote('http://127.0.0.1:9515', DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');
RemoteWebDriverServer
The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server. The server will always run on the machine with the browser you want to test.
wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
java -jar selenium-server-standalone-2.53.0.jar
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub',des
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python爬蟲(chóng)selenium和phantomJs使用方法解析
- Python中使用 Selenium 實(shí)現(xiàn)網(wǎng)頁(yè)截圖實(shí)例
- Python使用selenium實(shí)現(xiàn)網(wǎng)頁(yè)用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能
- python編程使用selenium模擬登陸淘寶實(shí)例代碼
- selenium+python自動(dòng)化測(cè)試之使用webdriver操作瀏覽器的方法
- 淺談python爬蟲(chóng)使用Selenium模擬瀏覽器行為
- Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè)
- python使用selenium實(shí)現(xiàn)批量文件下載
- Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法
- python使用selenium登錄QQ郵箱(附帶滑動(dòng)解鎖)
- Python使用Selenium模塊模擬瀏覽器抓取斗魚(yú)直播間信息示例
- Python selenium的基本使用方法分析
相關(guān)文章
python實(shí)現(xiàn)自動(dòng)打卡的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)自動(dòng)打卡的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-10-10
python基于socket函數(shù)實(shí)現(xiàn)端口掃描
這篇文章主要為大家詳細(xì)介紹了python基于socket函數(shù)實(shí)現(xiàn)端口掃描,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
對(duì)python中l(wèi)ist的拷貝與numpy的array的拷貝詳解
今天小編就為大家分享一篇對(duì)python中l(wèi)ist的拷貝與numpy的array的拷貝詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python之使用adb shell命令啟動(dòng)應(yīng)用的方法詳解
今天小編就為大家分享一篇Python之使用adb shell命令啟動(dòng)應(yīng)用的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
解決tensorboard多個(gè)events文件顯示紊亂的問(wèn)題
今天小編就為大家分享一篇解決tensorboard多個(gè)events文件顯示紊亂的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Pytest執(zhí)行unittest TestSuite(測(cè)試套件)的實(shí)現(xiàn)方法
TestSuite一直是unittest的靈活與精髓之處,在繁多的測(cè)試用例中,可以任意挑選和組合各種用例集,這篇文章主要介紹了Pytest執(zhí)行unittest TestSuite(測(cè)試套件)的實(shí)現(xiàn)方法,需要的朋友可以參考下2021-08-08

