Selenium environment + element positioning method

selenium and webdriver

Selenium is a tool for web testing where the tests run in the browser as if a real user were doing it by hand. Supports all major browsers

WebDriver encapsulates the native API provided by the browser, making it a more object-oriented Selenium WebDriver API.
Using this set of APIs, you can control the opening and closing of the browser, open web pages, operate interface elements, control cookies, and also operate browser screenshots, install plug-ins, set proxy, configure certificates, etc.

 Environment build

1. Install the selenium module for Python, pip install selenium

2. Download the corresponding browser driver 

Chromedriver

 Use selenium to visit Baidu and search

Anti-crawler settings

ui automated operation process

Select interface elements

According to the characteristics of the element: ID, Name, Class, Tag, etc.

According to element characteristics and relationships: css, xpath

User Interface Elements 

Input operations: click, input text, drag and drop, etc.

Output operation: Get various attributes of the element

Analyze and process data obtained on the interface

element positioning method

find_element_by_id: Match search by ID, only return one matched element

find_element_by_name: search for a match by name, and return only one matched element

find_element_by_xpath: Match search through xpath, only return one matched element

find_element_by_link_text: Search through the link content, and only return one matched element

find_element_by_partical_link_text: Perform matching search through partial link content, and only return one matched element

find_element_by_tag_name: search for a match by tag name, and return only one matched element

find_element_by_class_name: Matching search by class name, only returns one matched element

find_element_by_css_selector: Matching search through CSS selector, only returns one matched element

It is worth noting that the above method will only match the search and only get the first element. In addition to the above methods for finding a single element, Selenium also defines methods for finding multiple elements:

find_elements_by_name: Search by name and return a list of all matched elements

find_elements_by_xpath: Search for matches by xpath and return a list of all matched elements

find_elements_by_link_text: Search through the link content and return a list of all matched elements

find_elements_by_partical_link_text: Perform matching search through partial link content, and return a list of all matched elements

find_elements_by_tag_name: search by tag name and return a list of all matched elements

find_elements_by_class_name: Search by class name and return a list of all matched elements

find_elements_by_css_selector: Matching search through CSS selectors, returning a list of all matched elements

Notice:

1. When positioning according to class_name, sometimes you will encounter compound classes, that is, there is a space in the middle of the class attribute. The class attribute is special. The space in the middle of the class attribute is an interval symbol, indicating that an element has multiple class attributes Name, at this time, you can choose any one when positioning the element (unique positioning is not guaranteed)

2. Selenium does not provide the function of judging whether an element exists, so when you need to judge whether an element exists or not, direct positioning may report an error. We can match a list of elements. If the list is empty, the element does not exist. If the list is not empty, the element exists.

Skill, when positioning elements, sometimes you will encounter some elements that are particularly difficult to locate. At this time, you can use the following mode to locate them step by step and narrow the positioning range step by step.

ele = driver.find_elements_by_xpath("//div[@id='category-block']//ol/li")
b = ele.find_elements_by_xpath('.//li[@class=\'subcate-item\']//span')

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

insert image description here

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!   

Guess you like

Origin blog.csdn.net/2301_78276982/article/details/132408575