SoFunction
Updated on 2024-11-19

python3 selenium automated testing powerful CSS positioning methods

Advantages of ccs: css versus xpath syntax is more concise than xpath, and positioning speed is faster than xpath.

Disadvantages of css: css does not support the use of logical operators to position, while xpath support. css positioning syntax in various forms, compared to xpath is more difficult to remember.

The css positioning is recommended to be used more often, this positioning method is very powerful, fast and accurate positioning. As for the difficult to remember, use familiar with it, for the diligent, this is not a problem.

CSS_selector common symbols:

# denotes id
. Indicates class
> denotes child elements, hierarchies

1. Positioned by the id attribute:

find_element_by_css_selector("Attribute value for #id")

Example: find_element_by_css_selector("#kw")

2. Located by the class_name attribute:

find_element_by_css_selector("Attribute values for .class")

Example: find_element_by_css_selector(".s_ipt")

3. Positioning by other attributes:

find_element_by_css_selector("[attribute='attribute value']")

find_element_by_css_selector("[attribute=attribute_value]") ---------------- Note that the attribute value is not quoted here

Example 1: find_element_by_css_selector("[name='kw']")

Example 2.1: find_element_by_css_selector("[style='display']")

Example 2.2: find_element_by_css_selector("[style=display]") --------- Note that the attribute value is not quoted here!

The purpose of the two examples here is to emphasize that it doesn't matter whether the quotation marks are added here or not, they don't affect the positioning.

4. Positioning through paternity:

If you don't know someone's ID number, name, cell phone number and other information when you can't get in touch, but you know someone's dad's cell phone number, at this time you can find someone through his dad. That is to say, you can find someone through his father ("his father [phone=cell phone number]>myself").

Translated into terms it means that when we locate an element and we find that there is no attribute value that uniquely identifies the element, then we can consider using the father tag in combination with an attribute to locate the element. For example:

find_element_by_css_selector("attribute value of input#id > subordinate tag")

Example: find_element_by_css_selector("input#kw>div")

5. Positioning through the grandfather relationship:

If his dad doesn't have a cell phone number either, at this point you can go up to his grandfather. That is to say, you can find someone through ("his grandfather's [phone=cell phone number]>his father's>himself").

Translated into terms it means that when we locate an element and find that there is no attribute value in the father tag that uniquely identifies the element, then we can consider using the grandfather tag in combination with an attribute to locate the element. For example:

Example: find_element_by_css_selector("input#kw>divs>div#ko")

Above this python3 selenium automated testing Powerful CSS positioning method is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.