Web automated testing -Webdriver element of waiting

Webdriver --- elements waiting (waiting display)

 

  • Conceptual elements waiting

Because the network reasons, sometimes have to wait a long time to appear on the page, it will affect the look elements and positioning elements.

Display is waiting for the judgment related to wait for a certain element

(Waiting display, corresponding to a wait for a specific element, wait until it appears so far)

Implicit waiting without waiting for a certain element, global element waiting

(Implicit not wait for the individual elements, except to wait for a fixed time)

 

 

  • Related modules

WebDriverWait wait for the display elements must be used

Expected_conditions conditions expected class (which can be called the method comprising, waiting for display)

NoSuchElementException waiting for implicit exception is thrown

By positioning elements for

 

From selenium import webdriver

From selenium.webdriver.common.by import By

From selenium webdriver.support.ui import  WebDriverWait

From selenium.webdriver.support import exected_conditions as EC

From selenium.common.exceptions import NoSuchElementException

 

 

  1. Display wait

Case:

Detection Baidu search page button is present, there is just enter the keyword "selenium" and click Search

 

from  selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from time import sleep

 

driver=webdriver.Firefox()

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

driver.find_element_by_css_selector("#kw").send_keys("selenium")

sleep(2)

element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"su")))

element.click()

sleep(3)

driver.quit()

 

 

Note: in the third line of code "as EC" represents the aliased

Line 11 shows within 5 seconds, every 0.5 seconds a check element, if found. Search button element

 

 

2, implicit wait

 

 

from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException

from time import sleep,ctime

 

driver=webdriver.Firefox()

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

sleep(2)

# Implicit wait 5 seconds

driver.implicitly_wait(5)

 

try:

    # Print the current time, in seconds chronograph

    print(ctime())

    # Wait for the following two elements

    driver.find_element_by_css_selector("#kw").send_keys("selenium")

    driver.find_element_by_css_selector("#su").click()

# If unusual, abnormal'll print out

except NoSuchElementExceptionas as msg:

    print(msg)

finally:

    Printing is finished

    print(ctime())

 

sleep(2)

driver.quit()

Source: Thousand Feng software testing

Guess you like

Origin blog.csdn.net/IT_studied/article/details/90269180