Selenium webdriver--Automatic Learning (1) __web

Source: Internet
Author: User

Webdriver (Selenium2) is an automated test tool for Web applications that provides a friendly set of APIs that are easier to understand and use than the Selenium 1 (SELENIUM-RC) API, Its readability and maintainability are also greatly improved. Webdriver is simply a set of class libraries that do not rely on any test framework, and do not need to start other processes or install other programs, other than the necessary browser drivers, and do not need to start the service as Selenium 1 does.

In addition, the technical solutions adopted by the two are also different. Selenium 1 runs JavaScript in the browser to test, while Selenium 2 controls the browser directly through native browser support or browser extensions.

Selenium 2 is developed for each browser and replaces JavaScript embedded in the Web application under test. Tight integration with browsers supports the creation of more advanced tests and avoids the limitations of the JavaScript security model. In addition to the support from browser vendors, Selenium 2 uses operating system-level calls to simulate user input. Webdriver Support

Firefox (Firefoxdriver)

IE (Internetexplorerdriver)

Opera (Operadriver)

Chrome (Chromedriver)

and Safari (Safaridriver)

It also supports mobile application testing for Android (selendroid) and iphone (Appium). In addition, the Selenium 2 also includes htmlunit based interface-free implementations, called Htmlunitdriver, and WebKit based UI-free browser phantomjs. The Selenium 2 API is available through

Java

C#

Php

Python

Perl

Ruby

such as programming language access, which enables developers to create tests using their common programming language.


Small test: Automatic login Firefox browser, using Baidu, search for the keywords needed
Environment Preparation: Need to install selenium pip install Selenium first
#coding: UTF8

From selenium import Webdriver
Import Time #调入time函数
Browser = Webdriver. Firefox () #使用火狐浏览器
Browser.get ("http://www.baidu.com") #需要访问的URL地址, search with Baidu
#time. Sleep (0.3) #休眠0.3 seconds
browser.find_element_by_id ("kw"). Send_keys ("Python") #搜索框中输入python, if Chinese need to add U "China" in front
browser.find_element_by_id ("su"). Click () #提交
Time.sleep (3) # Hibernate 3 seconds
Browser.quit () #退出浏览器


Use the above program can be automated login Firefox browser, search for the specified keyword, note: Firefox browser version should not be too high, otherwise there will be driving problems, such as jump to the URL address input box, but has been unable to enter, we suggest to replace the lower version of the Firefox browser
The most commonly used commands:

Commmand Description
Driver.get ("URL") Navigating to Applications
Element.sendkeys ("Inputtext") Enter some text input boxes
Element.clear () Empty content from input box
Select.deselectall () This will deselect all options on the first selection on the page:
Select.selectbyvisibletext ("some text") Select the OPTION with the input specified by the user.
Driver.switchto (). Window ("Windowname") Moving the focus from one windows to another
Driver.switchto (). FRAME ("FrameName") Swing from frame to frame
Driver.switchto (). Alert () Helps in handling alerts
Driver.navigate (). to ("URL") Navigate to the URL
Driver.navigate (). Forward () To Navigate forward
Driver.navigate (). Back () To Navigate back
Driver.close () Closes the current Browser associated with the driver
Driver.quit () Quits the driver and closes the associated window of that driver.
Driver.refresh () Refreshes the current page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.