Eight methods of Selenium element positioning (recommended collection)

Automation generally requires four steps: get elements, operate elements, get returned results, assert (whether the returned results are consistent with the expected results), and finally automatically generate a test report. Selenium provides 8 methods for locating elements: id, name, class name, link text, xpath, css selector, tag name, partial link tex.

These eight element positioning methods are expressed in python language as:

find_element_by_id()

find_element_by_name()

find_element_by_class_name()

find_element_by_tag_name()

find_element_by_link_text()

find_element_by_partial_link_text()

find_element_by_xpath()

find_element_by_css_selector()

Take Baidu's page as an example:

Open the Baidu homepage, as shown in Figure 1, click F12, click the arrow next to the viewer and slide the mouse to the Baidu search input box, click the input box, the blue mark below is the element attribute of the input box

<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

1. Locating elements by id

From Figure 1, you can see that the input box has an id attribute: id="kw", here you can locate this element through its id attribute, and transmit characters to the input box through send_keys().

2. Locate elements by class_name

From Figure 1, you can see that the input box has a class attribute: class="s_ipt", here you can locate this element through its class attribute, and transmit characters to the input box through send_keys().

3. Locate elements by name

From Figure 1, you can see that the input box has a name attribute: name="wd", here you can locate this element through its name attribute, and transmit characters to the input box through send_keys().

4. Locate elements by tag

From Figure 1, we can see that the input box has a tag (label) attribute: input, here you can locate this element through its tag attribute, and transmit characters to the input box through send_keys(). But tags are often not unique, so they are not commonly used in practical applications.

5. Locating elements by link_text

Use the locator, click the "News" button to locate the element, as shown in Figure 2.

<a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>

You can see that "News" in Figure 2 has an href attribute of "http://news.baidu.com", which is a hyperlink, and this element can be located using link_text.

6. Locate elements by partial_link_text

If the link string is too long, all input will affect the beauty of the code and is prone to errors. At this time, partial_link_text can be used to fuzzy match and intercept a part of the string to locate the element. As shown in Figure 2, our positioning news can be written as follows.

7. Locating elements by xpath

The above six methods are all located through a certain attribute of the element, but if an element does not have the above attribute or the attribute is not unique, we can use xpath to locate the element.

Use the browser debugging function to locate the line where the element is located, right->Copy-->Copy XPath.

8. Locating elements through CSS selector

The CSS method and XPATH are completely different syntaxes. We first find the css of the search box through the locator.


If the article is helpful to you, remember to like, bookmark, and add attention. I will share some dry goods from time to time...

END Supporting Learning Resources Sharing

Finally:  In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】

加入我的软件测试交流qq群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

insert image description here

How to obtain the full set of information:

 

Guess you like

Origin blog.csdn.net/m0_58026506/article/details/131227478