Such as Baidu search box:
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
Baidu news link:
<a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
1.1.1 Ordinary positioning
id:find_element_by_id(“kw”)
name: find_element_by_name(“wd”)
class:find_element_by_class_name(“s_ipt”)
tag: find_element_by_tag_name("input") (same as button, generally not used)
link:find_element_by_link_text(“新闻”)
partial link: find_element_by_ partial_link_text("smell") (take part of the text link)
1.1.2 Xpath positioning
1.1.2.1 Absolute path
From /html to input.
1.1.2.2 Element attributes
copy xpath://*[@id="kw"]
find_element_by_xpath(” //*[@id="kw"]”)
find_element_by_xpath(” //input[@id="kw"]”)
Note: Attributes in xpath can also be located by name, class, and so on.
1.1.2.3 Parent properties
If the element to be located does not have an attribute value, it can be found through the parent or higher-level attribute.
find_element_by_xpath(” //span[@class="bg_s_ipt_wr"]/input”)
1.1.2.4 Logical Operators
Multiple attributes of multiple elements are not unique, and an element cannot be located through one attribute, but can be located through multiple attributes.
find_element_by_xpath(” //input[@id=’kw’ and @class=’su’] /span/input”)
1.1.3 css positioning
See the W3CSschool website for details.
CSS path copy method: chrome: F12, copy -- copy selector
Such as Baidu search box
<span class="bg s_ipt_wr quickdelete-wrap">
<span class="soutu-btn"></span>
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
<a href="javascript:;" id="quickdelete" title="清空" class="quickdelete" style="top: 0px; right: 0px; display: none;">
</a>
</span>
1.1.3.1 Class:.
class:find_element_by_css_selector(“.s_ipt"”)
. Indicates that the element is located through the class attribute.
1.1.3.2 Id:#
id:find_element_by_css_selector("#kw")
# Indicates that the element is located by the id attribute.
1.1.3.3 Tag name
Tag name: find_element_by_css_selector("input") (high repeatability, low positioning accuracy)
1.1.3.3.1 Parent-child relationship positioning
find_element_by_css_selector("span>input")
1.1.3.3.2 Property Location
find_element_by_css_selector(“[name=’kw’]”)
find_element_by_css_selector(“[autocomplete=off]”)
find_element_by_css_selector(‘[type=”submit”]’)
1.1.3.3.3 Combined positioning
find_element_by_css_selector("form.fm>span>input.s_ipt") (parent-child relationship & class) find_element_by_css_selector("form.fm>span>input#kw") (parent-child relationship & id)
1.1.4 Comparison of Xpath and CSS Positioning
Target |
XPath |
CSS 3 |
all elements |
//* |
* |
all P elements |
//p |
p |
all child elements of the p element |
//p/* |
p > * |
Get element by ID |
//*[@id='foo'] |
#foo |
Get element by class |
//*[contains(@class,'foo')] 1 |
.foo |
an element with an attribute |
//*[@title] |
*[title] |
the first child of all P elements |
//p/*[0] |
p > *:first-child |
all P elements that have child element a |
//p[a] |
can not achieve |
next sibling element |
//p/following-sibling::*[0] |
p + * |
id:find_element(By.ID,“kw”)1.1.5 By定位
- name: find_element(By.NAME,“wd”)
- class:find_element(By.CLASS_NAME,“s_ipt”)
- tag: find_element(By.TAG_NAME, "input") (same as button, generally not used)
- link:find_element(By.LINK_TEXT_text,“新闻”)
- partial link: find_element(By. PARTIAL_LINK_TEXT, "smell") (take part of the text link)
- Xpath:find_element(By.XPATH,“//*[@id=’kw’]”)
- CSS:find_element(By.CSS,” #kw”)
Note: Before trying By, you need to introduce By:
from selenium.webdriver.common.by import By