Selenium: screenshot showing waiting

Keywords: Python Selenium Lambda network

1, Show wait (conditional wait)

common problem:

  1. The location is clearly right. Why can't the running code find the location.
  2. The location is clearly right. If the location is found, why is the text information empty?

Analysis reason:

  1. frame not processed
  2. The page rendering speed is slower than the code of automatic test, and the page is located before it is rendered
  3. Asynchronous request. Before the back end returns to the front end, the automatic test code gets the page text data

Some students' solutions:

  1. Some students think, then I add time.sleep()
  2. Set global implicit wait, dr.implicitly_wait()

Analysis solution:

  1. Solution 1: we can't determine the exact forced waiting time (code sleep time), which will greatly increase the instability of automation code, especially the impact of network fluctuations, which will slow down the page loading speed
  2. Although global waiting can solve some positioning problems, such as the display of label attributes to be located, if it is like asynchronous request, it needs to wait for the corresponding with text information and render it on the page. It is not sure that the page has rendered the required text information when the automatic code execution gets the text information

What is display wait?

  • When the conditions are met, do not wait

2, Show wait related classes

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        """Constructor, need a WebDriver Instance and timeout in seconds
            - driver - afferent webdriver Instance object
            - timeout - Timeout in seconds
            - poll_frequency - Program sleep time, default 0.5 second
            - ignored_exceptions - Ignored exception if calling until or until_not The exception in this tuple is thrown during the,
            //If the exception is thrown out of the tuple, the code will be interrupted and an exception will be thrown. By default, only NoSuchElementException is available.

           //Use example:
            from import WebDriverWait \n
            element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
                        until_not(lambda x: x.find_element_by_id("someId").is_displayed())
    def until(self, method, message=''):
        """Call the incoming method within the specified timeout until the returned result is true, otherwise throw the timeout exception"""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            if time.time() > end_time:
        raise TimeoutException(message, screen, stacktrace)
    def until_not(self, method, message=''):
        """And unitl On the contrary, there is not much explanation, which is basically not used in the project"""
        end_time = time.time() + self._timeout
        while True:
                value = method(self._driver)
                if not value:
                    return value
            except self._ignored_exceptions:
                return True
            if time.time() > end_time:
        raise TimeoutException(message)

Commonly used

See the source code for the specific usage of each class and write it very clearly

# Judge that the page title is equal to the expected value

# Judge that the page title contains the expected string

# Judge that the current url is equal to the expected url

# Determine whether the current url contains the expected string
url_matches  # Regular match re
url_contains # Include in

# Judge that the current url is not equal to the expected url

# Judge the success of element presentation and positioning

# Judge the appearance of elements (not necessarily able to locate, the size of elements that can be located must be greater than 0)

# Judge that the obtained text information contains the expected text information

# Judge that the information to get the attribute value contains the expected text information

# Judge iframe can be switched, if it is true, switch, otherwise return False

# Judgment elements can be clicked

# Judge element selected

# Judge the number of windows

# Judge new window open

# Warning box display

Not commonly used


3, Show waiting

from selenium import webdriver
from import By
from import expected_conditions as EC
from import WebDriverWait

driver = webdriver.Chrome()
locator_of_search = (By.CSS_SELECTOR,'#kw')
driver.find_element(*locator_of_search).send_keys('Test display waiting')

Posted by arsitek on Wed, 15 Jan 2020 22:44:46 -0800