This article is participating in Python Theme Month. See the link for details

1. Introduction of Selenium

Selenium is an automated testing tool for testing web sites. It supports a variety of browsers including Chrome, Firefox, Safari, and the phantomJS interface less browser.

2. Supports multiple operating systems

For example, Windows, Linux, IOS, and Android.

3. Install the Selenium

pip install Selenium
Copy the code

4. Install the browser driver

Selenium3.x invoke browser must have a WebDriver driver file

5.Selenium provides eight methods for locating elements:

id name class name tag name link text partial link text xpath css selector

In fact, it is not necessary to memorize all, but when it is used, it will be checked. And these search methods can be switched back and forth, using whichever is convenient.

Find_element_by_id Find_elements_by_id Is located by element ID find_element_by_name Find_elements_by_name is located by element name Find_element_by_xpath Find_elements_by_xpath Find_element_by_link_text Find_elements_by_link_tex Find_element_by_link_tex find_element_by_link_tex find_element_by_link_tex Find_element_by_partial_link_text Find_elements_by_partial_link_text Locates find_element_by_tag_name by partial link Find_elements_by_tag_name Locates by tag find_element_by_class_name locates by class name Find_elements_by_css_selector Find_elements_by_css_selector is performed using a CSS selector

6. Some ways to control browser operations

Set_window_size () Sets the size of the browser. Back () Controls the browser back. Forward () Controls the browser forward. Refresh () Refreshes the current page Get_attribute (name) gets the element’s attribute value is_displayed() Sets whether the element is visible to the user. Size Returns the element’s size. Text Gets the element’s text

7. Mouse events

A mouse simulation is a must for Web automation testing

ActionChains(driver) createActionChains object context_click(); move_to_element(above) Drag_and_drop () drag_and_drop() move_to_element(above) Perform mouse hover action context_click() Perform () performs all the actions stored in ActionChains, which can be interpreted as a commit action for the entire operationCopy the code

8. Keyboard events

Send_keys (keys.back_space) Delete key (BackSpace) send_keys(keys.space) SPACE send_keys(keys.tab) TAB key (TAB) Send_keys (keys.escape) Rollback key (Esc) Send_keys (keys.enter) ENTER key (ENTER)Copy the code

9. Switch between multiple forms

In Web applications, frame/ IFrame form is often used to nest pages. WebDriver can only identify and locate elements on a page, but cannot directly locate elements on the page embedded in frame/ IFrame form. In this case, you need to switch the currently located body to the embedded page of the frame/iframe form using the switch_to.frame() method.

Switch_to.frame () switches the currently located body to the frame/iframe form’s embedded page switch_to.default_content() jumps back to the outermost page

10. Multi-window switching

Window_handles Returns handles to all Windows in the current session switch_to.window() is used to switch to the corresponding window, similar to switch_to.frame() in the previous section, The former is used for switching between different Windows, while the latter is used for switching between different forms.

Want a deeper understanding, get more source code cases. Please move to the public number: poetic code.