Contents of this chapter
I. Introduction
Selenium is a tool for Web application testing.
Selenium tests run directly in the browser, just as real users in the same operation. Supported browsers including IE, Firefox, Safari, Chrome, Opera and so on.
In the reptile to simulate normal user access web pages and fetch data.
Second, the installation (for example to Chrome)
-
Browser installed
The windows installation is not to say, the following server (Centos7) installation steps
#下载安装包到同级目录下 wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm #安装 yum install ./google-chrome-stable_current_x86_64.rpm
-
Selenium installation
pip install selenium
-
Driver Download
selenium is invoked by ChromeDriver Chrome browser (the other major browsers also have a corresponding drive), so you need to download ChromeDriver, and ChromeDriver version requires Chrome version corresponds with the version wrong, then it will run error.
(1) View Chrome browser version
Windows系统查看方法
: Open the Chrome browser> three upper right corner> Settings> About Chrome
Centos7 查看方法
:google-chrome --version
(2) download the appropriate version of the driver
下载地址:
Chrome browser driver (ChromeDriver): http://npm.taobao.org/mirrors/chromedriver/
Firefox browser driver (GeckoDriver): https://github.com/mozilla/geckodriver/releases
IE browser driver: HTTP: //selenium-release.storage.googleapis.com/index.html
Edge browser drive: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Opera browser driver: HTTPS: / /github.com/operasoftware/operachromiumdriver/releases
PhantomJS browser driver: https://phantomjs.org/
找到相应的版本:
can see my Chrome version78.0.3904.108
, find the corresponding version of the above URL, the corresponding large version (example: 78) that is can.
Inside a four files, corresponding to the first three corresponding system (not case 64 or 32),notes.txt
document illustrates the supported versions and updates(. 3) ChromeDriver installation
Mac / Linux : Once downloaded unzip the file moves to the
/usr/local/bin
directory, you can use the normal
Windows : After downloading unpack, move the file to a configuration environment variable folder, such as your Python installation folder.
Third, simple to use
This is an open Baidu page, enter example 'blunt brothers' in the input box.
import time
from selenium import webdriver
# 打开一个Chrome浏览器
driver = webdriver.Chrome() #Chrome浏览器
#driver = webdriver.Chrome(r'ChromeDriver路径') #还可以指定路径
#driver = webdriver.Firefox() # Firefox浏览器
#driver = webdriver.Ie() # IE浏览器
#driver = webdriver.Edge() # Edge浏览器
#driver = webdriver.Opera() # Opera浏览器
#driver = webdriver.PhantomJS() # PhantomJS浏览器
# 请求百度首页
driver.get('https://www.baidu.com')
time.sleep(5) #方便演示,等待五秒
#找到输入框位置,并输入'平头哥儿',百度特性(输入完内容会自动查)
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('平头哥儿')
time.sleep(5) #方便演示,等待五秒
driver.quit() #关闭全部窗口
Selenium anthology Portal:
title | Brief introduction |
---|---|
Python crawler - Selenium (1) easy to install and use | Details of installation and simple to use Selenium is dependent on the environment of Windows and Centos7 |
Python crawler - Selenium (2) and positioning elements common methods WebDriver | Details of the positioning element 8 ways and cooperate click and enter, submit, using the method of obtaining information assertion |
Python crawler - Selenium (3) common method of controlling the browser | Details of using a custom browser window or full-screen size, browser control back, forward, refresh your browser and other methods of |
Python reptile - Selenium (4) configuration parameters startup items | Details of the configuration parameters Selenium startup items including no interface mode, the browser window size, the browser User-Agent (request header), etc. |
Python reptile - Selenium (5) mouse events | Details of use of right-click, double click, drag, hover, etc. |
Python reptile - Selenium (6) key events | Details of operation of the keyboard, includes almost all common keycaps and key combinations |
Python crawler - Selenium (7) multi-window switch | Selenium is described in detail how to implement the freedom to switch between different windows |
Python crawler - Selenium (8) frame / iframe nested form page | Details of how to switch from the current positioning of the body frame / iframe embedded in a page form |
Python crawler - Selenium (9) alert box (pop) Processing | Details of how to locate and deal with many types of warning popups |
Python crawler - Selenium (10) treated drop-down box | Details on how to locate and deal with flexible drop-down box |
Python reptile - Selenium (11) file upload | Details of how elegant by send_keys () the specified file upload |
Python reptile - Selenium (12) for login Cookies, Cookies automatically log in and add | Details of how to obtain and use Cookies Cookies for automatic logon |
Python crawler - Selenium (13) element disposed wait | Details how elegant set of elements waiting time, to prevent the program from running too fast positioning element failure |
Python crawler - Selenium (14) screen shot | Details on how to use the screen shot |
Python reptile - Selenium (15) closes the browser | Close the window detailed describes two differences |
Welcome Message Tucao