Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of 8 methods of locating elements in Selenium

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Brief introduction

When we do web automation testing, the most fundamental thing is to manipulate the elements on the page, first we need to be able to find these elements, and then we can manipulate them. Tools or code cannot distinguish the elements on the page with the naked eye as we testers do. So we locate them based on elements.

First, view the page elements

Open the home page of Baidu (button F12) with Google browser, or click > more tools > developer tools in the upper right corner, you can see the html code of the entire page.

2. Introduction of the method

1.id location: find_element_by_id ()

From selenium import webdriver

Import time

Driver = webdriver.Chrome ()

# Windows window maximization

Driver.maximize_window ()

Driver.get ("https://www.baidu.com/")"

Driver.find_element_by_id ("kw") .send_keys ("python")

2.name location: find_element_by_name ()

Driver.find_element_by_name ("wd") .send_keys ("python")

3.class location: find_element_by_class_name ()

Driver.find_element_by_class_name ('python') .send_keys ("python")

4.tag location: find_element_by_tag_name ()

Driver.find_element_by_tag_name ('body') .get_attribute ("class")

5.link location: find_element_by_link_text ()

Driver.find_element_by_link_text ('map'). Click ()

6.partial_link location: find_element_by_partial_link_text ()

Driver.find_element_by_partial_link_text ('hao1') .click ()

7.xpath location: find_element_by_xpath ()

Driver.find_element_by_xpath ('/ / * [@ id= "kw"]') .send_keys ("python")

8.CSS location: find_element_by_css_selector ()

Driver.find_element_by_css_selector ('# kw') .send_keys ("python")

Location method selection

1. Choose a simple and stable positioning method.

2.. The common method is to locate id, name, class_name, xpath,css_selector.

3. When a page element has an id attribute, try to use id to locate it. If not, choose another location method (the measured id element is not unique).

4.css_selector is fast, so it is recommended to use it.

5. Consider linkText or partialLinkText when locating hyperlinks: note, however, that the text changes frequently, so it is not recommended.

6.xpath is the most powerful. But the execution is slow, because you need to find the entire DOM, so use it as little as possible. Xpath is used only when there is really no way out.

7. Locate the elements xpath and css_selector, which can be copied with the right mouse button so that there is no error.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report