Selenium study Notes

Source: Internet
Author: User
Tags element groups selenium grid

Selenium-webdriver (Python) Learning notes

I. Related principles and knowledge

Selenium is a web-based automated testing tool that has a number of advantages, including free, lightweight, multi-lingual, multi-platform, and support for the execution of distributed test cases.

The Selenium family has roughly four members, namely Selenium Rc,seleniumide,selenium grid,selenium Webdriver. The author mainly studies the selenium webdriver. Selenium Remote Control is a proxy server that can send Selenium test programs written in various programming languages to the specified browser and run, and its contents use jetty as webserver, The proxy of RC can solve the same-origin policy restriction when JS is called. When programming selenium using languages such as Java/c#/ruby, the Selenium API actually sends commands to the browser through the RC Proxy service. Selenium IDE only for FF, has a good record function, at the same time can be a series of operations directly into the relevant various code, easy to use, but can only be used for FF, but powerful, into code to facilitate learning, the author is very helpful. Selenium Webdriver is selenium1.x added webdriver after the new product, also known as selenium2.x, powerful and lightweight, for each language, as long as the introduction of the relevant package can be used.

The principle of Remote-control:

1.webdriver when the target browser is started, it will be bound to the specified port as a remote server for Web driver with this launched browser instance

The 2.Client end sends HttpRequest to the remote server's listening port protocol via Commandexcuter.

3.Remote server needs to rely on native browser components to convert browser native calls

Two. Selenium-webdriver installation and start-up

1. Install Python and Setuptools

2. Install Pip

3. Install Selenium:

Start the DOS command as follows: Pip install Selenium

Detection: Enter the Python interpreter, enter from selenium import Webdriver

4. Download Chromedriver, unzip the downloaded file to Chrome's installation directory, and add the Chromedriver directory to the path.

5. Launch the Chrome browser:

Edit in script:

From selenium import Webdriver

From time import sleep//ingest times, to show the effect, the browser sleeps for 3 seconds and then shuts down

Dr = Webdriver. Firefox ()

Sleep (3)

Dr.close ()

6. Close the browser: Quit () and close (), quit indicates the option to close the page, and close indicates that the browser is completely closed.

Three. Simple operation of Selenium+webdriver

1. There are two ways to access the URL, one is direct get and one is in navigate () mode.

If you visit Google: Driver=webdriver. Chrome ()

Driver.get ("http://www.google.com.hk")

2. Get the title:driver.title of the current URL

3. Positioning elements:

To manipulate an object, you need to understand or even crawl the elements in the object, and the elements in the object are attributes, and Webdriver provides many ways to locate objects, as follows:

* ID HTML Tag id attribute

* Name HTML Tag Name property

* Class_name The class attribute of the HTML tag

* CSS selector used by the Css_selector element

* Link_text Linked display text

* Partial_link_name Links Show part of text

* Tag_name Label Signature

* XPath

Suppose you have a Web page with the input box code as follows:

<input id= "kw" class= "S_ipt" type= "text" maxlength= "+" Name= "WD"
Autocomplete= "Off" >

Here's how it works:

Id:driver.find_element_by_id (' kw ')//Select the input box with the ID of KW

Name:driver.find_element_by_name (' WD ')

Class_name:driver.find_element_by_class_name (' S_ipt ')

Css_selector:driver.find_element_by_css_selector (' #kw ')

Link_text:driver.find_element_by_link_text(‘吧’)

 

Location of XPath:

XPath is a language that locates elements in an XML document. Because HTML can be seen as an implementation of XML,
So selenium users use this powerful language to locate elements in Web applications. XPath uses a path expression to select a node in the XML document. A node is selected by a path or step.

The most common expressions are as follows:

NodeName selects all child nodes of this node.

/select from the root node.

Selects the nodes in the document from the current node that matches the selection, regardless of their location.

. Select the current node.

.. Selects the parent node of the current node.

@ Select Properties.

Examples use:

Bookstore selects all child nodes of the bookstore element.

/bookstore

Select the root element bookstore.

Note: If the path starts with a forward slash (/), this path always represents the absolute path to an element!

Bookstore/book selects all book elements that belong to the bookstore child element.

Book selects all book child elements, regardless of their position in the document.

Bookstore//book selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore.

@lang Select all attributes that are named Lang.

4. Add hibernation

Sometimes in order to better show the results, you need to add sleep time, this operation only need to introduce time package to add the relevant code.

Import time

Time.sleep (2)

By adding implicitly_wait () it is convenient to implement the smart wait, which is implicitly waiting for an element to be discovered or a command to complete, and this method needs to be called only once per session.

5. Set the browser size

Maximized: Driver.maximize_window ()

Setting the length of the width: driver.set_window_size (480,500)

6. Operate the browser's forward, back

Methods of forward and backward Driver.back () and Driver.forward ()

7. Mouse click and keyboard input

Mouse case action, enter selenium in the above input box and search

The code is as follows:

driver.find_element_by_id ("kw"). Send_keys ("Selenium")
Time.sleep (2)
#通过 submit () to operate
driver.find_element_by_id ("Su"). Submit ()

How to use keyboard keys

When you need to use keyboard input, you need to introduce a package in the following way

Fromselenium.webdriver.common.keys Import Keys

The specific methods used are as follows:

.. driver.find_element_by_id (' kw '). Send_keys (Keys.enter)

Complex usage of mouse events

In automated testing, it is often necessary to right-click, double-click the operation, complete these two actions, need to introduce the Actionchains class. Introduced in the following way:

.. From Selenium.webdriver.common.action_chains import Actionchains

The following are the specific actions:

.. Fei=driver.find_element_by_xpath (...)

Actionchains (Driver). Context_click (FEI). Perform ()

Actionchains (Driver). Double_click (FEI). Perform ()

8. Positioning elements in a frame page

When the page contains other HTML files, when processing the internal HTML, you need to use the Switch_to_frame function to point the Webdriver object to the frame page, when the frame page is finished, you can pass the Switch_to_default_ The content function returns to the main page.

9. Locating element Groups

Find_elements series functions, such as a set of checkboxes in a list, can be used when we want to confirm the number of elements or need to find a particular element from some similar element. Operation is as follows

Checkboxs=driver.find_elements_by_xpath (..)

.. For checkbox in checkbox

Checkbox.click ()

Checkboxs[0].click ()

10. In the process of automated testing, sometimes the target element is not easy to locate, we can first locate its parent element, and then look for the target element.

11. Handling Prompt/alert/confirm

The message handling of JS native prompt/alert/confirm is similar to working with frame, using the Switch_to_alert method to switch objects, unlike the return object that is the operation Switch_to_alert when handling alert.

The following are the simple examples of processing:

Fei_alert=driver.find_element_by_xpath (..)

Fei_alert.click ()

Alert=driver.switch_to_alert ()

12. Processing pop-up windows

The pop-up window is not a dialog box generated by JS, but a new browser window that is generated when a new link is opened, which needs to be used to Switch_to_window. This function accepts two parameters, one is the window handle and the other is the name of the newly opened window.

13. Call JS

The implementation of JS generally have two scenarios, one is to execute JS directly on the page, and the other is to execute JS on an already positioned element. It uses Execute_script (..) Method to execute.

Case:

Js= "var Q=document.getelementbyid (" ")

Driver.execute_script (JS)

14. Control Browser scroll bar

Sometimes we need to control the scroll bar on the page scroll bar, but the scroll bar is not the element on the page, this time we need to use JS to help the operation.

Case: After entering selenium in Baidu, scroll the page to the bottom, then scroll to the top of the relevant function code as follows:

Js= "varq=document.documentelement.scrolltop=10000"

Driver.execute_script (JS)

Js= "Varq=document.documentelement.scrolltop=0"

Driver.execute_script (JS)

Four automated test Code small test

#-*-Coding:utf-8-*-

From selenium import Webdriver

From Selenium.webdriver.common.by Importby

From Selenium.webdriver.common.keysimport Keys

From Selenium.webdriver.support.ui Importselect

From Selenium.common.exceptions importnosuchelementexception

From Selenium.common.exceptions importnoalertpresentexception

Import UnitTest, time, re

Import OS

Driver=webdriver. Firefox ()

Driver.get ("http://wenwen.sogou.com/")

Driver.maximize_window ();

Driver.find_element_by_name (' Ask '). Click ()

Time.sleep (2)

driver.find_element_by_id (' S_login '). Click ()

Time.sleep (2)

Driver.switch_to.frame (' Login_iframe ')

driver.find_element_by_id (' u '). Send_keys (' 123123 ')

driver.find_element_by_id (' P '). Send_keys (' 123123 ')

Time.sleep (5)

driver.find_element_by_id (' Login_button '). Click ()

Time.sleep (2)

driver.find_element_by_id ("Questionwrap"). Click ()

Time.sleep (2)

driver.find_element_by_id ("Questionwrap"). Clear ()

Time.sleep (2)

driver.find_element_by_id ("Questionwrap"). Send_keys (U "Why is it so magical to have an affair?")

Time.sleep (5)

driver.find_element_by_id ("Submitbutton"). Click ()

Time.sleep (2)

Driver.close ();

Selenium study Notes

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.