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

Wait.py

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        """Constructor, need a WebDriver Instance and timeout in seconds
           :parameter:
            - 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 selenium.webdriver.support.ui 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:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        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:
            try:
                value = method(self._driver)
                if not value:
                    return value
            except self._ignored_exceptions:
                return True
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message)

expected_conditions.py

Commonly used

See the source code for the specific usage of each class and write it very clearly
/Python/Python36/Lib/site-packages/selenium/webdriver/support/expected_conditions.py

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

# Judge that the page title contains the expected string
title_contains 

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

# 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
url_changes

# Judge the success of element presentation and positioning
visibility_of_element_located

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

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

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

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

# Judgment elements can be clicked
element_to_be_clickable

# Judge element selected
element_to_be_selected
element_located_to_be_selected

# Judge the number of windows
number_of_windows_to_be

# Judge new window open
number_of_windows_to_be

# Warning box display
alert_is_present

Not commonly used

presence_of_element_located
presence_of_all_elements_located
visibility_of_any_elements_located
visibility_of_all_elements_located
invisibility_of_element_located
invisibility_of_element(invisibility_of_element_located)
staleness_of
element_selection_state_to_be
element_located_selection_state_to_be

3, Show waiting

demo.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
locator_of_search = (By.CSS_SELECTOR,'#kw')
WebDriverWait(driver=driver,timeout=10,poll_frequency=0.5).until(EC.visibility_of_element_located(locator_of_search))
driver.find_element(*locator_of_search).send_keys('Test display waiting')

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