欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在python中使用selenium的示例

 更新時間:2017年12月26日 09:23:17   作者:虞大膽  
這篇文章主要介紹了如何在python中使用selenium的示例,selenium提供了一個通用的接口,可模擬用戶來操作瀏覽器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近基于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)的繼承者.

  1. WebDriver提供了更簡單和簡潔的接口,克服了Selenium-RC API一些限制.
  2. 相比Selenium 1.0,WebDriver是面向?qū)ο笫降姆?
  3. WebDriver驅(qū)動瀏覽器更有效率,提供了比Selenium 1.0更多的功能
  4. 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與C互相調(diào)用的方法詳解

    python與C互相調(diào)用的方法詳解

    這篇文章主要給大家介紹了關于python與C互相調(diào)用方法的相關資料,文中通過示例代碼詳細介紹了動用的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-07-07
  • Python四大金剛之元組詳解

    Python四大金剛之元組詳解

    這篇文章主要介紹了Python的元組,小編覺得這篇文章寫的還不錯,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python實現(xiàn)的排列組合、破解密碼算法示例

    Python實現(xiàn)的排列組合、破解密碼算法示例

    這篇文章主要介紹了Python實現(xiàn)的排列組合、破解密碼算法,結合實例形式分析了Python排列組合、密碼破解相關數(shù)學運算操作技巧,需要的朋友可以參考下
    2019-04-04
  • Python區(qū)塊鏈交易類教程

    Python區(qū)塊鏈交易類教程

    這篇文章主要為大家介紹了Python區(qū)塊鏈交易類的示例詳解教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Flask框架運用Ajax實現(xiàn)數(shù)據(jù)交互的示例代碼

    Flask框架運用Ajax實現(xiàn)數(shù)據(jù)交互的示例代碼

    使用Ajax技術網(wǎng)頁應用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個頁面,這使得程序能夠更快地回應用戶的操作,本文將簡單介紹使用AJAX如何實現(xiàn)前后端數(shù)據(jù)通信
    2022-11-11
  • python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解

    python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解

    在本篇文章里小編給大家分享了關于python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解內(nèi)容,有興趣的朋友們可以學習下。
    2020-12-12
  • python析構函數(shù)用法及注意事項

    python析構函數(shù)用法及注意事項

    在本篇文章里小編給大家整理的是一篇關于python析構函數(shù)用法及注意事項,有需要的朋友們可以學習參考下。
    2021-06-06
  • 基于python寫個國慶假期倒計時程序

    基于python寫個國慶假期倒計時程序

    國慶假期快到了,想查查還有幾天幾小時到假期,這對程序員小菜一碟,輕輕松松用python寫個倒計時程序(天、時、分、秒),助你熬到假期
    2021-09-09
  • Python中的類屬性與實例屬性的區(qū)別和用法

    Python中的類屬性與實例屬性的區(qū)別和用法

    在Python中,類屬性和實例屬性是面向?qū)ο缶幊痰暮诵母拍钪?它們允許存儲和管理對象的數(shù)據(jù),并影響對象的行為,本篇文章中,會學習到類屬性和實例屬性的概念、區(qū)別以及如何在Python中使用它們,同時提供大量的示例代碼來更好地理解它們的作用和用法,需要的朋友可以參考下
    2023-11-11
  • Python使用正則表達式報錯:nothing?to?repeat?at?position?0的解決方案

    Python使用正則表達式報錯:nothing?to?repeat?at?position?0的解決方案

    今天在使用python 正則模塊匹配字符串時遇到了這個問題,分享給大家,這篇文章主要給大家介紹了關于Python使用正則表達式報錯nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下
    2023-03-03

最新評論