[Learning experience] Quick review of automated testing tool selenium

(1) What is selenium

1. Selenium is an automated testing tool. In human terms, it means using code to control the browser.

2. 8 components of selenium script

        ① Instantiate the browser object

        ② Get browser information

        ③ Request to access the page

        ④ Find positioned elements

        ⑤ Operating elements

        ⑥ Action chain/behavior chain

        ⑦ Get element information

        ⑧ Delay waiting

(2) webdriver module

1. WebDriver object

        ① Instantiate the WebDriver object driver

                Browser classification

                        webdriver.Chrome()

                        webdriver.Firefox()

                        webdriver.Edge()

                        webdriver.Safari()

                Browser options

                        options

        ②Attributes

                driver.page_source  web page source code                 

                driver.current_urlThe  url address requested by the current tab or window

                driver.window_handles  window handle list

        ③ Method

                driver.get()

                Function: Send a request to a URL

                driver.find_element()

                Function: Locate a specified element

                Parameters: by=specify the positioning method, value=specify the specific value

                Targeting

                        By this class should be imported from selenium.webdriver.common.by import By

                By.ID

                        Positioning based on tag id attribute

                By.NAME

                        Positioning based on tag name attribute

                By.CLASS_NAME

                        Positioning based on label class attribute

                By.TAG_NAME

                        Target based on tag name

                By.LINK_TEXT

                        Position based on the text wrapped by the label

                By.CSS_SELECTOR

                        Position based on CSS selectors

                By.XPATH

                        Locate according to Xpath path query language

                Return value: a WebElement object

                driver.find_elements()

                Function: Locate all specified elements

                Parameters: by=specify the positioning method, value=specify the specific value

                Return value: a list consisting of WebElement objects

                driver.get_cookie()

                Function: Get a cookie named xxx

                Parameters: name=the name of the cookie you want to get

                driver.get_cookies()

                Function: Get all cookies

                Return value: a list of dictionaries

                driver.execute_script()

                Function: Execute JavaScript script

                Open new tab

                        "window.open('http://httpbin.org/ip')"

                scroll scroll bar

                        Move to the bottom of the page

                                “window.scrollTo(0, document.body.scrollHeight)”

                                The document.body.scrollHeight property is to get the bottom height of the current HTML document

                Move to coordinates relative to current location

                        “window.scrollBy(0, 700)”

                Move to absolute position

                        “window.scrollTo(0, 1200)”

                        The absolute position of the entire window is x=0, y=1200

                driver.close()

                Function: Close the current tab or window

                driver.quit()

                Function: Close the entire browser

                driver.forward()

                Function: Go to the next page

                driver.back()

                Function: Go back to the previous page

                driver.set_window_size()

                Function: Set browser window size

                Parameters: width and height

                driver.implicitly_wait()

                Function: Implicitly waits for the specified length of time you pass in

④ switch_to module

method

driver.switch_to.window()

  1. Function: Open a window (just one more window on the desktop)
  2. Parameters: window handle or window name

driver.switch_to.new_window()

  1. Function: Open a new tab or window
  2. Parameters: 'tab' new tab, 'window' new window

2. WebElement object

① Attribute

        tag.text

        Get the text content in the label

        tag.id

        Get node ID

        tag.location

        Get the relative position of a node in the page

        tag.tag_name

        Get the label name of the node

        tag.size

        Get the width and height of the node

② Method

        tag.send_keys()

        Function: Send value to the specified tag element

        Parameters: the key value you want to send

        tag.clear()

        Function: Clear the content sent in the element

        tag.click()

        Function: Click this element

        tag.get_attribute()

        Function: Get the attribute value of the label node

        Parameter: the name of the attribute you want to get

        tag.find_element()

        Function: You can continue to search for the desired tag among the positioned tags.

        Both the WebDriver object and the WebElement object have this method

3. ActionChains object

① Instantiate ActionChains object actions

        ActionChains(driver)

② Method

        actions.move_to_element()

        Function: Move the mouse to the specified element

        Parameters: specified element

        actions.send_kyes_to_element()

        Function: Enter a value into the element

        Parameters: element, value

        actions.click()

        Function: click on the element

        Parameters: the element you want to click

        actions.drag_and_drop()

        Function: Drag the source element to the target element position

        Parameters: the initial position of the source, the position after the target is dragged

        actions.perform()

        Function: Execute the defined action

4. WebDriverWait object

① Instantiate the WebDriverWait object wait

        from selenium.webdriver.support.ui import WebDriverWait

        It is a waiting object and needs to be passed in the driver object and the waiting time.

        wait = WebDriverWait(driver, 100)

② Method

        until()

        Function: Pass in the waiting condition, and the function will wait until the condition occurs or the maximum time you set before is reached before exiting the wait.

        Parameters: excepted_conditions expected conditions

(3) expected_conditions module

1. Expectation condition function

presence_of_element_located()

Function: Determine whether the expected element appears

Parameters: Pass in a locator which is a tuple, for example (By.ID, 'id_name')

element_to_be_clickable()

Function: Determine whether the expected element appears and can be clicked

Parameters: Pass in a locator or WebElement object

alert_is_present()

Function: Determine whether a warning box appears

Guess you like

Origin blog.csdn.net/qq_39780701/article/details/128229943