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

How to realize the location of Web Page elements in Selenium

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to locate the elements of the web page in Selenium. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In addition to finding an element based on its id value, Selenium provides a number of ways to find an element:

As you can see from the figure above, Selenium provides nearly 20 find_ element family methods for us to find elements in the page, including id, name, class name, css selector, link text, tag signature, xpath, and so on.

The ways to locate a single matching element are:

Find_element_by_id

Find_element_by_name

Find_element_by_xpath

Find_element_by_link_text

Find_element_by_partial_link_text

Find_element_by_tag_name

Find_element_by_class_name

Find_element_by_css_selector

The ways to locate multiple matching elements are:

Find_elements_by_name

Find_elements_by_xpath

Find_elements_by_link_text

Find_elements_by_partial_link_text

Find_elements_by_tag_name

Find_elements_by_class_name

Find_elements_by_css_selector

We can locate the page elements we need according to the different circumstances of different pages.

3.2Positioning through the id attribute

If you know the id attribute value of the element, you can use the find_element_by_id () method to locate the element, which returns the first element whose id attribute value matches that position.

The structure of the search box on Baidu's home page is as follows:

Ele returns a corresponding element element:

If no element matches the passed id value, a NoSuchElementException exception will be thrown:

Run the code and throw an exception because there is no matching id value:

3.3.Positioning through the name attribute

If you know the name attribute value of an element, you can use the find_element_by_name () method to get the first element that matches the name attribute value:

Ele returns the matching element element:

C:\ Python35\ python.exe E:/pythonproject/selenium_env/code/2.py

If you locate a value of the name property that does not exist, a NoSuchElementException exception is also thrown:

3.4 element positioning through Xpath

XPath is a language used to find nodes in XML documents. Because HTML can be the implementation of XML (XHTML), we can use this powerful language to locate elements in web pages. XPath extends and supports a simple way to locate through the id or name attribute, and provides a variety of new operations, such as finding a third check box on the page.

One of the reasons for using Xpath is that sometimes the element we need to locate on the page does not have the id attribute and the name attribute, so we can use Xpath to locate the element in an absolute path, or locate the parent element and then get the child element through the id or name attribute value.

I will not introduce the knowledge of Xpath here. Students who need to take a look at the website or tutorials that specifically introduce Xpath, such as:

W3schhool Xpath tutorial

Here we briefly introduce how to quickly get the Xpath path of the page element through the browser. Open the web page debugging console in the browser, right-click on the element you want to locate, and an options bar appears. Select "Copy XPath" in "copy":

In this way, we can also locate the Baidu home search box through the XPath path:

3.5 locate elements by marking signatures

When we want to locate an element by its tag name, we can use the find_element_by_tag_name () method, which returns the first element with a given tag name:

Here, we locate directly through the element tag signature of input. Because the first input on the home page of Baidu is a search box, we can also locate:

If there is no match, a NoSuchElementException exception is also thrown.

3.6 locate the element by the class class name

If we want to locate by the value of the class attribute of the element, we can use the find_element_by_class_name () method. It will return the first element that matches, and if there is no matching element, it will also throw a NoSuchElementException exception:

Here, we locate the search button on the home page of Baidu through the class class name.

3.7Positioning elements through CSS selector

The CSS selector is a syntax that locates an element by its CSS attribute value, and we can use the find_element_by_css_selector () method to locate the element through the css selector:

The absolute syntax of the CSS selector can be obtained through the "Copy" in the browser debugging console:

3.8 locate the element through the link label text

In addition to the above element positioning, we can also locate the element through the text on the a tag, using the find_element_by_link_text () method.

At the top of Baidu's home page, there is a row of links, as shown below:

If we need to navigate to the link element of the Map, we can do this:

In this way, the status of success comes to the element:

Similarly, if there is no match, a NoSuchElementException exception will be thrown:

Can't match the element whose link text is "Mr. of the State":

The above is how to achieve the positioning of web page elements in the Selenium shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report