Element Waiting for Python-based Web Automation (Selenium)

basic introduction

Most web applications today use AJAX technology. When the browser loads the page, the elements on the page may not be loaded at the same time, which makes the positioning of the elements difficult. If an ElementNotVisibleException occurs due to a delay in loading an element, the stability of the automation script will be reduced, and the instability caused by this problem can be improved by setting the element to wait.

Actual operation

1. Display waiting

Display waiting is that WebDriver waits for a certain condition to continue to execute, otherwise it throws a TimeoutException when the maximum is reached. Not much nonsense, go directly to the code:

# -*- coding: utf-8 -*-

from seleniumimport webdriver

import time

from selenium.webdriver.common.by import By

from  selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()

driver.get('https://www.baidu.com/')

element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'kw')))

element.send_keys('selenium')

time.sleep(5)driver.quit()

First you need to import a "from selenium.webdriver.support import expected_conditionsas EC", here named "EC", easy to use. The WebDriverWait class is the wait method provided by WebDriver. Within the set time, by default, it will check whether the current page element exists at regular intervals. If it cannot be detected after the set time, an exception will be thrown. The specific format is as follows:

1.1 WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

    driver: browser driver

    timeout: the maximum timeout time, the default is in seconds

    poll_frequency: interval (step) time for detection, default is 0.5 seconds

    ingored_exceptions: exception information after timeout, throws NoSuchElementException by default

1.2 until(method,message='')

    Call this method providing the driver as a parameter, knowing that the return value is True.

    【until_not(method,message='')   

        Call this method providing the driver as a parameter, knowing that the return value is False.

1.3EC.presence_of_element_located()

Rename expected_conditions to EC through the as keyword, and call the presence_of_element_located() method to determine whether the element exists.

2. Implicit wait

Implicit waits are much simpler than explicit waits. Implicit wait is to wait for an element on the page to load for a certain period of time. If the element is not loaded after the set duration, a NoSuchElementException is thrown. WebDriver provides implicitly_wait(), which is set to 0 by default. Of course, you can also use time.sleep() to wait. code show as below:

# -*- coding: utf-8 -*-

from selenium import webdriver

import time

from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()

driver.implicitly_wait(10)

driver.get('https://www.baidu.com/')

try:

print(time.ctime())

driver.find_element_by_id('kw').send_keys('selenium')

except NoSuchElementException as e :

print (s)

finally:

print(time.ctime())

time.sleep(5)

driver.quit()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324466171&siteId=291194637