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 locate the dynamic id and class of Python selenium

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to locate the dynamic id and class of Python selenium". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are reasons why elements cannot be located in python-dynamic id, class.

No new page opened, no alert, no frame, added wait time, but still can't locate the element? Many of the element attributes you want to locate are dynamic. That is, every time you open a page, the element attributes such as id or class of this element are dynamically generated. When the code executes, the attribute of the element is different from that before. Of course, it is impossible to locate with the previous attribute value, and a NoSuchElementException will be thrown.

So, how do you tell if an element is dynamic? Very simple, generally see that there is a concatenation of a string of numbers in the element attributes, many of them are dynamic. Want to tell the difference? Refresh the reader and then look at the element, the number string in the attribute value has changed, that is, the dynamic attribute.

As follows:

...

How to locate this type of element?

1. Locate based on other attributes

If there are other fixed attributes, the first consideration is to locate according to other attributes of the element. There are so many ways of positioning that there is no need to hang on a tree.

two。 Position according to relative relationship

Locate according to its nearby parent node, child node and sibling node

3. Index positioning according to DOM sequence

This is very simple, find the index of the element in the main document or some level of parent node, and then easily locate it according to index, but this kind of method is not very uncertain, if you can locate it in other ways, it is more suitable.

4. Locate according to some element attributes

There are three very good ways to locate some attribute values for us in xpath:

Driver.find_element_by_xpath ("/ / div [contains (@ id, 'btn-attention')]")

Driver.find_element_by_xpath ("/ / div [starts-with (@ id, 'btn-attention')]")

Driver.find_element_by_xpath ("/ / div [ends-with (@ id, 'btn-attention')]") # this needs to end with' btn-attention'

Contain (aforme b) returns true if a contains string b, otherwise returns false

Starts-with returns true if a begins with the string b, otherwise returns false

Ends-with (a, b) returns true if an ends with the string b, otherwise returns false

It's important to note that different browsers have different support for xpath, such as IE, so don't be surprised that sometimes xpath can be located in one browser but not in another.

This is the end of the content of "how to locate the dynamic id and class of Python selenium". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report