Positioning element, selenium element 8 is provided in the positioning method:
(1) find_element_by_id (): html provisions, id must be unique in html, somewhat similar to the ID number
(2) find_element_by_name (): html specified, name is used to specify the name of the element, somewhat similar names
(3) find_element_by_tag_name (): to locate the signature elements
Html prescribed class name, class specified elements: (4) find_element_by_class_name ()
(5) find_element_by_link_text (): designed to locate text link
(6) find_element_by_partial_link_text (): is a supplement to link_text, some text links long, can take part of the link text positioning, as long as this part of the text is the only sign of this link
(7)find_element_by_css_selector()
(8)find_element_by_xpath()
==========================================================================================================
=========================================================================================================
http://www.baidu.com Home html source code
输入框: <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
OK button: <input type = "submit" id = "su" value = "Baidu it" class = "bg s_btn">
A row of links above:
<a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
<a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
<a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a>
<a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a>
<a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">贴吧</a>
<a href="http://xueshu.baidu.com" name="tj_trxueshu" class="mnav">学术</a>
<a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" onclick="return false;">登录</a>
<a href="http://www.baidu.com/gaoji/preferences.html" name="tj_settingicon" class="pf">设置</a>
<a href="http://www.baidu.com/more/" name="tj_briicon" class="bri" style="display: block;">更多产品</a>
---------------------------------------------------------------------------------------------------------------------------------------------------
(1) id positioned
find_element_by_id ( 'kw'): the input box
find_element_by_id ( 'su'): 'Baidu search' button
(2) name Location
find_element_by_name ( 'wd'): the input box
(3) class_name Location
find_element_by_class_name ( 's_ipt'): the input box
(4) tag_name Location
find_element_by_tag_name ( 'input'): the input box
(5) link_text Location
find_element_by_link_text ( 'News')
find_element_by_link_text ( 'map')
find_element_by_link_text ( 'video')
find_element_by_link_text('贴吧')
(6) partial_link_text Location
find_element_by_partial_link_text('新')
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(7) XPath Location
(A), the absolute path
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input").send_keys('中国')
Note: I go first to the last span of the path, coupled with the input, not directly copy the path above the last input
(B) using the positioning element attributes: input box: <input id = "kw" name = "wd" class = "s_ipt" value = "" maxlength = "255" autocomplete = "off">
OK button: <input type = "submit" id = "su" value = "Baidu it" class = "bg s_btn">
driver.find_element_by_xpath ( "// input [@ id = 'kw']") input box
driver.find_element_by_xpath ( "// input [@ id = 'su']") Search OK
// input an input label representing the current page;
id = 'kw' represents the value of this element is the id kW;
--------------------------------------------
You can also locate by name and class:
driver.find_element_by_xpath ( "// * [@ name = 'kw']") input box
driver.find_element_by_xpath ( "// * [@ class = 's_ipt']") input box
If you do not specify tag names, it can also be used instead of an asterisk, of course, XPath is not limited id, name, class three attribute values, attribute any element may be used as long as it can sign only one element;
driver.find_element_by_xpath("//input[@maxlength='255']") 输入框
driver.find_element_by_xpath("//input[@autocomplete='off']") 输入框
driver.find_element_by_xpath ( "// input [@ type = 'submit']") Search OK
(C), in conjunction with the attribute level
If an element does not have the value of this attribute uniquely identifies the element, then we can look for it on an element. If the parent can uniquely identify the elements, you can make use of them.
Plus property values Baidu input box is not available, you can look it up on a property:
driver.find_element_by_xpath("//span[@class='bg s_ipt_wr quickdelete-wrap']/input").send_keys('中国') 输入框
driver.find_element_by_xpath ( "// span [@ class = 'bg s_btn_wr'] / input"). click () searches the OK button
Note: This method has problems Note, use this query hint can not find the path (reason unknown)
If the parent element does not use value, it can continue to look up the parent element parent element:
driver.find_element_by_xpath ( "// form [@ id = 'form'] / span [1] / input"). send_keys ( 'Chinese') input box
driver.find_element_by_xpath ( "// form [@ id = 'form'] / span [2] / input"). click () searches the OK button
(Iv), using the logical operators
If a property of an element can not be uniquely determined, it can be connected to a plurality of logical operators to find the attributes:
driver.find_element_by_xpath ( "// input [@ id = 'kw' and @ class = 's_ipt']"). send_keys ( 'Chinese') input box
driver.find_element_by_xpath ( "// input [@ id = 'su' and @ type = 'submit']"). click () searches the OK button
(V), using the method contains
A method for matching string contains an attribute contained:
driver.find_element_by_xpath("//span[contains(@class,'s_ipt_wr')]/input").send_keys('中国') 输入框
driver.find_element_by_xpath ( "// input [contains (@ id, 'su')]"). click () searches the OK button
(Vi), using text () method
text () method for matching the text information displayed, similar to the implementation of the positioning link_text
driver.find_element_by_xpath("//a[text(),'新闻']")
driver.find_element_by_xpath ( "// a [contains (text (), 'news')]") with the use of two #
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Example illustration selector
.class .intro select all class "intro" element =
#id #firstname Select all id = "firstname" elements
* * Select all elements
All selected element p <p> element
element, element div, p selects all <div> element and <p> element
Select element element div p <div> all <p> element within an element
element> element div> p is the parent to select all <div> element <p> element
element + element div + p Select <p> element immediately after all the <div> element
[Attribute] [target] selects all elements with a target attribute
[Attribute = value] [target = -blank] used to select all target = "- blank" elements
(8) CSS positioning
(A), by positioning class
Dot (.) Represents a number of elements positioned by class
driver.find_element_by_css_selector ( ". s_ipt"). send_keys ( 'China') input box
driver.find_element_by_css_selector ( ". bg.s_btn"). click () Search OK button
NOTE: bg s_btn replaced with a space in the middle.
NOTE: class = 'bg s_ipt_wr quickdelete-wrap', such as this is called a composite class, composed of a plurality of class selectors,
Write laws positioning is: .bg.s_ipt_wr.quickdelete-wrap, with all spaces (dot) instead.
(B), by positioning id
driver.find_element_by_css_selector ( "# kw"). send_keys ( 'China') input box
driver.find_element_by_css_selector ( "# su"). click () Search OK button
(Iii), by positioning the tag name
In CSS, when positioning element with the tag name without any identification symbol, tag name can be used directly
driver.find_element_by_css_selector ( "input [name = wd]"). send_keys ( 'Chinese') input box
driver.find_element_by_css_selector ( "input [id = su]"). click () searches the OK button
(IV), by positioning properties
CSS may be used in any attribute positioning element, as long as they can uniquely identify the element properties;
Property value, it can be quoted, you can not add, but to pay attention to and quote the entire character were to take charge.
driver.find_element_by_css_selector ( "[autocomplete = off]"). send_keys ( 'Chinese') input box
driver.find_element_by_css_selector ( "[name = 'wd']"). send_keys ( 'Chinese') input box
driver.find_element_by_css_selector ( '[type = "submit"]'). click () searches the OK button
(V), by positioning the label hierarchy
driver.find_element_by_css_selector("span > input#kw").send_keys('中国')
driver.find_element_by_css_selector('span > input#su').click()
(Vi) a combination of positioning
driver.find_element_by_css_selector("form.fm > span > input.s_ipt").send_keys('中国')
driver.find_element_by_css_selector("form#form > span > input#kw").send_keys('中国')
(Seven), positioning more usage
==============================================================================================================
Sometimes we can not locate elements of possible causes:
1. Check whether the search target forget to play quotation marks;
2.Frame / Iframe can not locate the elements;
3.Xpath Description Cause: Solution: Xpath write a good path, chrome of F12-> html, ctrl + F to find, see if you can find.
4. The page has not loaded up, the operation of the elements on the page: The solution: import time module set wait time.
5. Dynamic Positioning less than id elements: Solution: If id is dynamic, it is best not to use, instead use xpath or otherwise positioned
6. The secondary positioning, such as pop-up Login: Workaround: Locate the pop-up box, and then navigate to the element pop-up box.
7. Positioning invisible elements:
[Emoji: 00a0] [emoji: 00a0] [emoji: 00a0] As Baidu login code lookup by name tj_login login elements, some of which are not visible,
So add a loop to determine, find visual elements (is_displayed ()) click Sign in.
8. browser compatibility problems, such as SPACE space operations Firefox will not identify, and Chrome will be able to identify respond.
9. Refresh the page can not be found or backward elements
After the page refreshes or backward, can not be directly used elements before positioning operation, it needs to be redefined before proceeding.