Python Selenium position page elements based on class

Source: Internet
Author: User
Tags xpath

In the daily page source code, we based on the element ID to locate is the most foolproof, the ID in a single page is not duplicated. But in practice, many front-end developers do not write ID attributes for each element. A typical HTML code is as follows:

1 <Divclass= "Sui-tips s-isindex-wrap sui-tips-exceedtipnews"style= "display:none; width:auto;"><Divclass= "Sui-tips-arrow"style= "left:15px;"><em></em></Div><Divclass= "Sui-tips-body">Call me too many times today,<BR>Let's have a little more of it tomorrow! ' (*∩_∩*)</Div></Div>

In this example, the outermost div does not have an id attribute, at which point the element can be positioned based on the class attribute. The common selenium based on class positioning elements are as follows:

First, driver.find_element_by_class_name ("classname")but a lot of times, many side-by-side elements such as a list form, class are shared with the same, such as:

At this point the Driver.find_elements_by_class_name ("classname") can come in handy, and the method can return a list of lists, so all methods for list are equally applicable. For example, if we know that the element you want to locate is nth in the page, you can position it like this:

Two

Driver.find_elements_by_class_name ("classname") [n] (note: Is elements, not element)

It should be noted that using the above method, even if there is only one element of the page, the resulting is still a list object, but the length is 1.

Of course, if you are familiar with the CSS method, you can also use CSS to locate, selenium is also supported, CSS, "." followed by the class name, a general wording is as follows:

Three

Driver.find_element_by_css_selector ('. dtb-style-1 '). Click ()

If your example is special enough, there are multiple classname of this element, and the above method can also use multiple "." For a parallel connection. Such as:

Driver.find_element_by_css_selector ('. Dtb-style-1.table-dragcolumns '). Click ()

There is also a way to support the case of multiple classes, or CSS property methods:

driver.find_element_by_css_selector ("[class= ' dtb-style-1 table-dragcolumns ']") separated by a space.

If you are not familiar with CSS properties, also matter, powerful Chrome browser can automatically help you to generate elements of XPath, CSS and other properties. Take Baidu Home Source example, in the page source files, positioning to the element, right-click, the effect is as follows:

Generated code after the copy, for this scenario can be directly used, but in view of the current page update is very frequent, it is recommended to simply learn under the selector of XPath, CSS, and other important methods, so we can write very flexible code, the page fine adjustment will be more adaptable.

Finally, we can also locate the element with a strong XPath, and if the element has more than one class, we only have to choose one to put in the XPath, otherwise the program will make an error. Examples are as follows:

Four

Driver.find_element_by_xpath ('//div[@class = ' u_sp ']/a[1] '). Click ()

In so many ways, XPath is the most flexible, because XPath has a lot of functions built in, and in some cases you may need to rely on this flexibility, and "flexibility" inevitably brings complexity. Nevertheless, I still encourage you, in the actual work, each method of positioning elements to learn, many times, you will find your careful writing method is not the spirit, this time to master a variety of positioning methods, it is particularly important.

With these skills, in the world of Python selenium, positioning elements based on class attributes will appear so easy.

Python Selenium position page elements based on class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.