Selenium2+python automation 44-element positioning parameterization (find_element)

foreword

There are eight methods for element positioning. This little friend who can read this article knows it, so is there a way to combine the eight positioning into one? That is to parameterize the positioning method, such as id, name.css, etc., as a parameter, so that only the parameters of the positioning method need to be maintained.

The editor once encapsulated this method by himself, and the final positioning method is written as follows: find_element("id=kw"), find_element("css=#kw"), this idea comes from the RF framework, and the equal sign is preceded by positioning method, the equals sign is followed by the element name.

In the past two days, I have nothing to do to check the source code of the positioning method, and I found a new continent. I couldn't help but want to share it with my friends.

一、find_element()

1. There is actually this method in selenium element positioning, but most of the time it is used in conjunction with the By method, as shown below

 

2. View the source code of the find_element method

1. What is the difference between find_element and find_element_by_xxx? Curiosity killed the cat, find this path: Lib\site-packages\selenium\webdriver\remote\utils.py

2. After opening the folder, it is found that the methods of determining find_element_by_xxx are all returned find_element methods, which means that the eight positioning methods are actually eight small branches.

 

3. By positioning method

1. Find this path: Lib\site-packages\selenium\webdriver\common\by.py

2. Open the by module, which is actually very simple, just a few string parameters.

3. Then the problem is simple. In fact, there is no need to take such a big detour to import this module. To be honest, I don't like to import this By at all, I always find it 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. The editor has been pursuing a simple and rude way, and then I will use the simplest method to locate

2. Summarize several positioning methods (note that there are spaces in the middle of the string)

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"

 

5. 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()

# 其它定位参考 交流QQ群:232607095
# t1 = driver.find_element("link text", "糯米").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 it, is there a feeling that there is no place to find a place to step through the iron shoes, and it takes no effort to get it. If you think it is helpful to you, just like it in the lower right corner, thank you my friend!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348263&siteId=291194637