Objective
Element positioning there are eight ways, this can see this piece of small partners know, then there is a method, you can put eight kinds of positioning into one? That is, the positioning of the way parameterization, such as ID,NAME.CSS, such as set as a parameter, so just maintain the parameters of the positioning method is OK.
The small series has itself encapsulated this method, the final positioning method is written like this: Find_element ("id=kw"), Find_element ("css= #kw"), this idea is derived from the RF framework inside, the equal sign is preceded by the positioning method, after the equal sign is the element name.
These two days idle nothing to see the source of positioning method, discovered the new world, can't help but want to share to small partners.
First, Find_element ()
1.selenium element Positioning In fact there is this method, but most of the time is combined by method use, such as
Second, view Find_element method source code
What's the difference between 1.find_element and find_element_by_xxx? Curiosity killed the cat, find this path: lib\site-packages\selenium\webdriver\remote\utils.py
2. Open the folder, found that, in fact, the method of Find_element_by_xxx is the return of the Find_element method, that is, the eight positioning method is actually eight small branch.
Third, by positioning method
1. Locate this path: lib\site-packages\selenium\webdriver\common\by.py
2. Open by this module, in fact, it is very simple ah, just a few string parameters.
3. Then the problem is simple, in fact, can not go around such a big detour to import this module Ah, to tell the truth, I do not like to import this by, always feel too cumbersome.
"""
The by implementation.
"""
Class by (object):
"""
Set of supported locator strategies.
"""
id = "ID"
XPath = "XPath"
Link_text = "link TEXT"
Partial_link_text = "Partial LINK TEXT"
Name = "Name"
tag_name = "Tag NAME"
Class_name = "Class NAME"
Css_selector = "CSS SELECTOR"
Four, positioning parameterization
1. Small series has been the pursuit of simple and rough way, then the simplest way to locate
2. Summarize the following several positioning methods (the middle of the string is a space to be noted)
By_id= "id"
By_xpath = "XPath"
By_link_text = "link text"
By_partial_text = "Partial link text"
By_name = "Name"
By_tag_name = "tag name"
By_class_name = "Class name"
By_css_selector = "CSS selector"
V. Reference code
# Coding:utf-8
From selenium import Webdriver
From selenium.webdriver.common.by Import by
Driver = Webdriver. Firefox ()
Driver.get ("https://www.baidu.com/")
Driver.find_element ("id", "kw"). Send_keys ("Yoyoketang")
Driver.find_element (' CSS selector ', ' #su '). Click ()
# Other positioning Reference QQ Group: 232607095
# t1 = driver.find_element ("link text", "glutinous rice"). Text
# Print T1
# t2 = driver.find_element ("name", "Tj_trnews"). Text
# Print T2
# t3 = driver.find_element ("Class name", "BRI"). Text
# print T3
After reading there is not a pale, marvelous feeling. Feel that you have help, in the lower right corner of a praise, thanks to small partners!
Selenium2+python Automation 44-element positioning parameterization (find_element)