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