基于Python的Selenium经验总结

作者:计算机知识

我们都明白,基于Web端的测验的底工框架是索要Selenium做首要支撑的,这里边给大家介绍下Web测验中央之基于Python的Selenium

操作蒙受

来自http://selenium-python.readthedocs.org/

Introduction
Python language bindings for Selenium WebDriver.
Theseleniumpackage is used to automate web browser interaction from Python.
-Home:http://www.seleniumhq.org
-Docs:https://seleniumhq.github.io/selenium/docs/api/py/api.html
-Dev:https://github.com/SeleniumHQ/selenium
-PyPI:https://pypi.python.org/pypi/selenium
-IRC:#seleniumchannel on freenode
Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer, PhantomJS), as well as the Remote protocol.
Supported Python Versions
·Python 2.6, 2.7
·Python 3.3
Installing
If you have pip on your system, you can simply install or upgrade the Python bindings:

Selenium 是用来测量检验 Web 应用程序用户分界面的常用框架。它是豆蔻梢头款用于运营端到端效能测验的超强工具。您能够行使五个编制程序语言编写测量检验,何况Selenium 能够在一个或五个浏览器中实践这几个测验。

  • Chrome版本: 60.0.3112.101
  • ChromeDriver:2.31 (Supports Chrome v58-60)
  • IDE:Pycharm
  • Python:3.6.2
  • Selenium:3.5.0

5. Waits

These days most of the web apps are using AJAX techniques. When a page is loaded to browser, the elements within that page may load at different time intervals. This makes locating elements difficult, if the element is not present in the DOM, it will raise ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting provides some time interval between actions performed - mostly locating element or any other operation with the element.

Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait makes WebDriver to wait for a certain condition to occur before proceeding further with executions. An implicit wait makes WebDriver to poll the DOM for a certain amount of time when trying to locate an element.

pip install -U selenium
  1. 安装python,这个。。。忽略,建议python2.7。

  2. 安装基于python的selenium重视包,命令:pip install selenium==2.53.6,你懂的。

景况设置

基于Python的Selenium经验总结。5.1. Explicit Waits

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Expected Conditions

There are some common conditions that are frequent when automating web browsers. Listed below are Implementations of each. Selenium Python binding provides some convienence methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable - it is Displayed and Enabled.
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

The expected_conditions module contains a set of predefined conditions to use with WebDriverWait.

Alternately, you can download the source distribution from PyPI (e.g. selenium-3.0.0.b2.tar.gz), unarchive it, and run:

3. 安装浏览器驱动包,推荐使用chrome浏览器的chromedriver.exe,对应chrome版本必必要对哦,不然运转不起来的,安装在哪?想放哪放哪,可是貌似是放在python的根目录下。下载地址:

  1. 将ChromeDriver放入Chrome.exe所在路线

5.2. Implicit Waits

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
pip install -U selenium
  1. 设置PyCharm 2.7左右版本,这几个无脑安装~然后可自定义分界面UI及编码风格,这些。。。忽视
![](https://upload-images.jianshu.io/upload_images/3245878-6a5a3618777fdc1a.png)

Note: both of the methods described above installseleniumas a system-wide package That will require administrative/root access to their machine. You may consider using a virtualenv to create isolated Python environments instead.
Example 0:
·open a new Firefox browser
·load the page at the given URL

1. 决定浏览器

  1. 将ChromeDriver所在目录增加到系统Path中
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
#coding=utf-8from selenium import webdriverdriver = webdriver.Chrome()driver.get("http://www.baidu.com")driver.maximize_window() #将浏览器最大化显示driver.set_window_size #设置浏览器宽480、高800显示" driver.back() #后退driver.forward() #前进 driver.close() #关闭chromedriver.quit() # 退出chrome
![](https://upload-images.jianshu.io/upload_images/3245878-d32b3a1f09545ab3.png)

Example 1:
·open a new Firefox browser
·load the Yahoo homepage
·search for “seleniumhq”
·close the browser

2. 目的的定势

  1. 设置Python,将Python路径加多到PATH选项打钩,点击Customiz installation
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title
elem = browser.find_element_by_name('p')  # Find the search box
elem.send_keys('seleniumhq'   Keys.RETURN)
browser.quit()

本文由bwin必赢发布,转载请注明来源

关键词: Python bwin开户网 selenium 经验 技术学习