seleniumpython实例_如何在python中使⽤selenium的⽰例最近基于selenium写了⼀个python⼩⼯具,记录下学习记录,⾃⼰运⾏的环境是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是⾯向对象式的服务.
WebDriver驱动浏览器更有效率,提供了⽐Selenium 1.0更多的功能
Selenium RC只能在单机上运⾏,WebDriver则提供了远程操作的功能
selenium基本使⽤
selenium运⾏需要什么
主要包括三部分:selenium selenium,浏览器driver,浏览器selenium selenium是⼀组通⽤的接⼝,⽽不同的浏览器提供其⾃⾝的driver(⼤部分是官⽅的),浏览器则被模拟控制操作的终端.
安装
pip install selenium --upgrade
apt-get install chromium-browser
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')
<('mail.sina');
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'})
selenium怎么使用
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(_capabilities())
driver = webdriver.Chrome(desired_capabilities=capabilities)
chromedriver运⾏⽅式
The ChromeDriver class不断的创建实例,会浪费很多的时间,可以通过两个⽅式解决.
使⽤ChromeDriverService
import selenium.webdriver.chrome.service as service
service = service.Service('/usr/bin/chromedrive')
service.start()
capabilities = { }
driver = webdriver.Remote(service.service_url, capabilities)
<('mail.sina');
print(driver.title)
开启单独的ChromeDriver服务
./chromedriver
driver = webdriver.Remote('127.0.0.1:9515', DesiredCapabilities.CHROME)
<('mail.sina');
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.
java -jar selenium-server-standalone-2.53.0.jar
from selenium import webdriver
from selenium.webdrivermon.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote( command_executor='127.0.0.1:4444/wd/hub',des
desired_capabilities=DesiredCapabilities.CHROME)
<('mail.sina');
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持脚本之家。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。