如何在python中使用selenium的示例
最近基于selenium寫了一個python小工具,記錄下學習記錄,自己運行的環(huán)境是Ubuntu 14.04.4, Python 2.7,Chromium 49.0,ChromeDriver 2.16
selenium簡介
selenium提供了一個通用的接口,可模擬用戶來操作瀏覽器,比如用于自動化測試等.
selenium的核心是WebDriver,它提供了一組接口,這些接口能夠操作各種跨平臺的瀏覽器.各大瀏覽器廠商.
各大瀏覽器廠商也支持Selenium,將其作為瀏覽器的一部分.
selenium工具集提供了WebDriver,Selenium IDE,Selenium-Grid等
Selenium 1.0 + WebDriver = Selenium 2.0
Selenium WebDriver是Selenium Remote Control(Selenium-RC)的繼承者.
- WebDriver提供了更簡單和簡潔的接口,克服了Selenium-RC API一些限制.
- 相比Selenium 1.0,WebDriver是面向?qū)ο笫降姆?
- WebDriver驅(qū)動瀏覽器更有效率,提供了比Selenium 1.0更多的功能
- Selenium RC只能在單機上運行,WebDriver則提供了遠程操作的功能
selenium基本使用
selenium運行需要什么
主要包括三部分: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
簡單的使用
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
可以通過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是構建在DesiredCapabilities之上的,為了使用DesiredCapabilities,必須知道capability的Key/value對.
chrome_options = Options() capabilities={} capabilities['platform'] = "WINDOWS" capabilities['version'] = "10" capabilities.update(chrome_options.to_capabilities()) driver = webdriver.Chrome(desired_capabilities=capabilities)
chromedriver運行方式
The ChromeDriver class不斷的創(chuàng)建實例,會浪費很多的時間,可以通過兩個方式解決.
使用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)
開啟單獨的ChromeDriver服務
./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');
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- python爬蟲selenium和phantomJs使用方法解析
- Python中使用 Selenium 實現(xiàn)網(wǎng)頁截圖實例
- Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
- python編程使用selenium模擬登陸淘寶實例代碼
- selenium+python自動化測試之使用webdriver操作瀏覽器的方法
- 淺談python爬蟲使用Selenium模擬瀏覽器行為
- Python使用Selenium+BeautifulSoup爬取淘寶搜索頁
- python使用selenium實現(xiàn)批量文件下載
- Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法
- python使用selenium登錄QQ郵箱(附帶滑動解鎖)
- Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例
- Python selenium的基本使用方法分析
相關文章
Flask框架運用Ajax實現(xiàn)數(shù)據(jù)交互的示例代碼
使用Ajax技術網(wǎng)頁應用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個頁面,這使得程序能夠更快地回應用戶的操作,本文將簡單介紹使用AJAX如何實現(xiàn)前后端數(shù)據(jù)通信2022-11-11python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解
在本篇文章里小編給大家分享了關于python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解內(nèi)容,有興趣的朋友們可以學習下。2020-12-12Python使用正則表達式報錯:nothing?to?repeat?at?position?0的解決方案
今天在使用python 正則模塊匹配字符串時遇到了這個問題,分享給大家,這篇文章主要給大家介紹了關于Python使用正則表達式報錯nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下2023-03-03