Python+selenium Several common positioning methods (integration)

Source: Internet
Author: User
Tags sleep xpath

The first part of the two articles here is the basic positioning method; The second section details the XPath positioning (. find_element_by_xpath).

The first part:

Below I will copy part of the article, to see the full content, please click the original address to view

Python:

1 Find the ID of the element.

From selenium import Webdriver

Browser = Webdriver. Firefox ()

browser.find_element_by_id ("element ' s ID")

2) is positioned by the name of the element.

Browser.find_element_by_name ("element ' s name")

3) classname by the name of the style sheet applied.

Browser.find_element_by_class_name ("Class name")

4 apply features through CSS.

Find_element_by_css_selector ("a[id= ' id ']")

5 through the linked text.

Browser.find_element_link_name ("link name")

6) through XPath positioning.

Browser.find_element_by_xpath ("//***[*= ' * *]")

After reading these, several improvements have been made to my code, and the experimental results show that the above methods are feasible and effective.

Here is a demonstration of several positioning methods that demonstrate access to the address bit mobile Web site address.

From selenium import webdriver
import time
import re
#做一个等待的通用方法
def Sleep (sec=3):
    time.sleep ( SEC)

Driver = Webdriver. Firefox ()
driver.get ("http://m.mail.10086.cn") Sleep
()
driver.find_element_by_name ("UserName"). Send_keys ("longname") Sleep
()
driver.find_element_by_css_selector (". Textbox_pw"). Send_keys ("Password" #使用class定位方式 Sleep
()
driver.find_element_by_id ("Loginbtn"). Click ()
Driver.current_url
matchobj = Re.match (R ' (^.*?) cn). *$ ', Line,re. M|re. I)
Print (Matchobj.group (1)) #输出当前的浏览器地址 (does not contain detailed addresses and parameters
) Driver.find_element_by_xpath (".//*[@id = ' logout ']/ A/i "). Click () Sleep
()
driver.find_element_by_link_text (" Re-Logon "). Click () Sleep
()
driver.quit ()

Part II:

Note: XPath locates multiple tab indices at the same level starting at 1 instead of 0

1. Absolute positioning:

This method is the simplest, with the specific format

Xxx.find_element_by_xpath ("absolute path")

Specific examples:

Xxx.find_element_by_xpath ("/html/body/div[x]/form/input") x represents the X div tag, note that the index starts at 1 instead of 0.

The disadvantage of this method is obvious, when the page element position changes, you need to modify, so it is not recommended.

2. Relative path:

Relative path, starting with '//', with the specified format

Xxx.find_element_by_xpath ("//Label")

Specific examples:

Xxx.find_element_by_xpath ("//input[x]") locates the X input tag, [x] can be omitted, the default is the first

The length and start position of the relative path is unrestricted, and you can also take the following methods

Xxx.find_element_by_xpath ("//div[x]/form[x]/input[x]"), [x] still can be omitted

("//*[@id = ' J_login_form ']/dl/dt/input[@id = ' J_password ']"

3. Label attribute positioning:

3.1 Tag attribute positioning, relatively simple, also requires attributes to be able to navigate to the only element, if there are more than one label of the same condition, the default is only the first, specific format

Xxx.find_element_by_xpath ("//Label [@ property = = ' property value ']")

Property criteria: The most common is id,name,class, and so on, there are no special restrictions on the categories of attributes, so long as it is possible to uniquely identify an element

Specific examples

Xxx.find_element_by_xpath ("//a[@href = '/industrymall/hall/industryindex.ht ']")
Xxx.find_element_by_xpath ("//input[@value = ' OK ']")
Xxx.find_element_by_xpath ("//div[@class = ' Submit ']/input")

When a property is not sufficient to uniquely differentiate an element, it is possible to take multiple conditional combinations, as illustrated in

Xxx.. Find_element_by_xpath ("//input[@type = ' name ' and @name = ' kw1 ']")

3.2 When the label attributes are few, not enough to uniquely differentiate elements, but there is a unique text value in the middle of the label, can also be positioned, its specific format

Xxx.find_element_by_xpath ("//Label [contains (text (), ' text value ')]")

Specific examples:

Xxx.find_element_by_xpath ("//iunpt[contains" (Text (), ' model: ')] "

Note: Copy this text as much as possible in HTML to avoid positioning failures due to characters that cannot be distinguished by the naked eye

3.3 Other attribute values if too long, can also take the fuzzy method positioning, directly on the example

Xxx.find_element_by_xpath ("//a[contains (@href, ' logout ')]")

3.4 XPath about the positioning of dynamic properties in Web pages, for example, ASP. NET application, there are four ways to generate the id attribute values dynamically:

A.starts-with Example: Input[starts-with (@id, ' Ctrl ')] parsing: matching property values starting with Ctrl

B.ends-with Example: Input[ends-with (@id, ' _username ')] parsing: matching attribute values ending with UserName

C.contains () Example: Input[contains (@id, ' userName ')] parsing: matching contains userName attribute values

Of course, if the above single method does not complete the positioning, you can also take a modular positioning similar ("//input[@id = ' kw1 ']//input[start-with (@id, ' Nice ']/div[1]/form[3])

The above is a normal situation, there are properties that can be positioned, when the attributes of an element and its combination is not enough to locate, we can use their siblings or parent nodes, such as various elements can be positioned to locate, first look at the methods supported in XPath:

1. Select all child elements of the current node

2, parent Select the current node's parents node

3, descendant Select all descendant elements of the current node (child, grandchild, etc.)

4, ancestor Select all ancestors of the current node (father, grandfather, etc.)

5. Descendant-or-self selects all descendant elements (child, grandchild, etc.) of the current node and the current node itself

6. Ancestor-or-self selects all ancestors of the current node (parent, grandfather, etc.) and the current node itself

Related Article

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.