CSS, XPath and other path positioning methods in the Web

Source: Internet
Author: User

Today less than eight o'clock to the company, to the earlier, while a bit of time, summed up under the Web CSS, XPath and other path positioning the way it!

A simple introduction to the positioning of XPath and CSS

Theoretical knowledge is not listed.

or use the homepage of the blog Park, directly attached to the code:

This is XPath.

 1#!/usr/bin/env python2#-*-Coding:utf_8-*-34From Learn_webdriverImport Webdriver5From seleniumImport Webdriver6From Selenium.webdriver.common.action_chainsImport Actionchains7from time import sleep 8 9 browser_chrome = Webdriver. Chrome (Webdriver.chrome ()) 10 browser_chrome.get ( "http./ www.cnblogs.com/") 11 12 Sleep (2) Span class= "Hljs-number" >13 actionchains (browser_chrome). Move_to_element (Browser_chrome.find_element_by_xpath ( Span class= "hljs-string" ".//li[@id = ' cate_item_2 ')"). Perform () 14 # mouse hover on the left "programming language" navigation bar 15 Browser_chrome.find_element_by_xpath ( 16 Sleep (2) 17 browser_chrome.quit ()       

XPath is a language for finding information in an XML document

The function that is used is Find_element_by_xpath

The XPath expression is summarized here:

An expression

Description

Case

Node name

Selects all child nodes under a node

Body

All child nodes under body

/

Select from the root node

Body/div

All DIV nodes under body

//

Match Selection Node

No consideration of location

Div

div node regardless of location

.

Current node

..

Parent node of the current node

@

Select Properties

.//li[@id = ' cate_item_2 ')

Li node and attribute id=cate_item_2

Here is the CSS

 1#!/usr/bin/env python2#-*-Coding:utf_8-*-34From Learn_webdriverImport Webdriver5From seleniumImport Webdriver6From Selenium.webdriver.common.action_chainsImport Actionchains7From timeImport Sleep 8 9 browser_chrome = Webdriver. Chrome (Webdriver.chrome ())Browser_chrome.get ("http://www.cnblogs.com/") Sleep (2)  Actionchains (Browser_chrome). Move_to_element (Browser_chrome.find_element_by_css_selector ("li[id = ' Cate_ Item_2 ']). Perform () # Hover over the "programming language" navigation bar on the left browser_chrome.find_element_by_css_selector ("a[ href = '/cate/python/'). Click ()sleep (2)browser_chrome.quit ()     

CSS is a little more flexible

The function used by CSS is Find_element_by_css_selector

CSS, XPath and other path positioning methods in the Web

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.