Selenium-webdriver API Command and operation--eight element positioning _selenium

Source: Internet
Author: User
Tags tag name xpath

Original address: Http://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

(This article is only for the Python part of the translation)

First of all, I have a point: my English is very poor, so this translation is only used to understand more deeply the use of webdriver.

First, get the page

The first step in using Webdriver is to open a page and usually call get to do it:

Driver.get ("http://www.google.com");
including the operating system/browser and many other factors, webdriver will not wait for the page to load itself. In some cases, webdriver may be out of control before the page is fully loaded. To ensure stability, you can use "explicit wait" and "implicit wait" to wait for the element to be positioned to appear on the page and then manipulate it.

Explicit wait:

Implicit wait:

Second, positioning elements

There are two ways to locate an element in Webdriver: By creating an instance or a page element. Each language has two ways of "find Element" and "Get Elements". The former returns the element object corresponding to the query, and throws an exception if the element is not found. The latter returns a list of multiple page elements, which are empty if there is no DOM element that matches the query.

The Find method has a by-anchored object, and the following is a list of how by:

1, by ID

This is the most efficient and preferred method of locating elements, and the common problem is that UI developers do not have unique or dynamically changing IDs, both of which should be avoided, and setting a class for an element is more appropriate than a dynamic ID.

Examples are as follows:

Html:

<div id= "Coolestwidgetevah" >...</div>
Python code:

element = driver.find_element_by_id ("Coolestwidgetevah")

or from

selenium.webdriver.common.by import by
element = Driver.find_element (by=by.id, value= "Coolestwidgetevah")

2, by Class Name

In this case, the class is the property of the DOM element, and usually the class name of many DOM elements is the same, so if you look for an element of the same class name, the method becomes a more appropriate choice.

Examples are as follows:

Html:

<div class= "Cheese" ><span>cheddar</span></div><div class= "cheese" ><span> Gouda</span></div
Python code:

cheeses = driver.find_elements_by_class_name ("cheese")

or from

selenium.webdriver.common.by import
by cheeses = driver.find_elements (by.class_name, "cheese")

3, by Tag Name

That is, the label name of the DOM element

Examples are as follows:

Html:

<iframe src= "..." ></iframe>
Python code:

frame = Driver.find_element_by_tag_name ("iframe")

or from

selenium.webdriver.common.by import by
frame = Driver.find_element (By.tag_name, "iframe")
4, by Name

Find an element by matching the element name property

Examples are as follows:

Html:

<input name= "Cheese" type= "text"/>
Python code:

Cheese = driver.find_element_by_name ("cheese")

or from

selenium.webdriver.common.by import by
cheese = Driver.find_element (by.name, "cheese")
5, by Link Text

Find hyperlink elements by visible text

Examples are as follows:

Html:

<a href= "Http://www.google.com/search?q=cheese" >cheese</a>>
Python code:

Cheese = Driver.find_element_by_link_text ("cheese")

or from

selenium.webdriver.common.by import
by Cheese = driver.find_element (by.link_text, "cheese")
6, by Partial Link Text

Find hyperlink elements by visible text blur

Examples are as follows:

Html:

<a href= "Http://www.google.com/search?q=cheese" >search for cheese</a>>

Python code:

Cheese = Driver.find_element_by_partial_link_text ("cheese")

or from

selenium.webdriver.common.by import by
cheese = driver.find_element (by.partial_link_text, "cheese")
7, by CSS

As the name suggests, it is a positioning strategy for CSS. Usually supported by local browsers, please get a list of common CSS selectors through the selectors CSS, and use sizzle if the browser does not support CSS queries. IE 6, 7, and ff3.0 currently use sizzle as a CSS query engine.

Note that each browser is different and the corresponding version of the CSS is different.

Examples are as follows:

Html:

<div id= "Food" ><span class= "Dairy" >milk</span><span class= "dairy aged" >cheese</
Python:

Cheese = Driver.find_element_by_css_selector ("#food span.dairy.aged")

or from

selenium.webdriver.common.by Import
by cheese = driver.find_element (by.css_selector, "#food span.dairy.aged")

8, by Xpath

Webdriver can use XPath functionality anytime, if the browser does not support XPath, and we do so, it will cause unexpected errors unless you understand the differences between different XPath engines.

Label and property name properties locally support XPath Htmlunit Driver lower-cased appears in HTML is Internet Explorer Driver lower-cased appears in HTML no Firefox Driver Case-insensitive now that HTML is somewhat abstract, see examples:

Html:

<input type= "text" name= "Example"/> <input type=
"text" name= "other"/>
Python code:

Inputs = Driver.find_elements_by_xpath ("//input")

or from

selenium.webdriver.common.by import by
inputs = Driver.find_elements (By.xpath, "//input")
The resulting (number) of matches are as follows:

XPath expression htmlunit Driver Firefox Driver Internet Explorer Driver//input 1 2 2//input 0 2 0 Sometimes, HTML elements do not need to explicitly display declarative properties because it has a A default value. For example, an input label does not require a type attribute because it defaults to text. The rule of thumb for using XPath in Webdriver is not to expect to be able to match these implicit attributes.

Description: Translation of CSS and XPath is very abstract, look at me confused, in fact, the actual operation is not so complex

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.