selenium waiting mechanism

Wait mechanism

Because you want to find the label due to the speed and other reasons has yet to load it, you direct access to the label, it is clear that given the existing simple and crude solution is to time.sleep(3)sleep for a few seconds, which is set thread to wait etc. after this label to load out, go use. So a simple, but relatively inflexible, because we do not know when this label to load it, you write about dead sleep a few seconds, it can, but if this label in a very short period of time to load out, and you also in sleep .......

Display waiting mechanism

Examples

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC  #判断
from selenium.webdriver.support.ui import WebDriverWait   # 显式等待
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10, 0.5)
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("Na_years")
driver.find_element_by_id("su").click()
wait.until(EC.presence_of_element_located((By.LINK_TEXT, 'Na_years - 博客园'))).click()  
#EC判断页面是否加载完成,加载完成就打开
time.sleep(3)
driver.quit()

parameter

the Selenium 提供了WebDriverWait class implements the waiting mechanism. Such receiving four parameters specify a wait mechanism.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
WebDriverWait(driver=driver, timeout=10, poll_frequency=0.5, ignored_exceptions=None)
  • driver, drive browser
  • timeout, the maximum timeout time (sec)
  • poll_frequency, polling detection time, how much time is detected every time, the default is 0.5 seconds
  • ignored_exceptions, abnormal information after a timeout, the default throws NoSuchElementException

method

  • until (self, method, message = ''), method is the need to provide the driver until returns True, used more.
  • until_not (self, method, message = ''), method is the need to provide the driver until returns False

Driver method used above for the expected_conditionsrenamed EC, and call its presence_of_element_locatedmethod of determining whether there is a specified element.
expected_conditionsAnalyzing the various modules provide:

  • presence_of_element_locatedDetermine whether an element is added to the dom tree, does not mean that certain elements visible
  • presence_of_elements_locatedDetermining whether there is at least one element is present in dom tree. For example, if there are elements of the class n pages are 'column-md-3', as long as there is an element exists, the method returns True

Implicit wait mechanism

Explicit waiting difference is that we can wait for the implicit object invocation drive directly through the browser, and usage is relatively simple:

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(time_to_wait=10)  # 只需要一个等待超时时间参数
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("Na_years")
driver.find_element_by_id("su").click()
print(driver.title)
driver.find_element_by_link_text("Na_years - 博客园").click()
time.sleep(3)
driver.quit()

implicitly_waitWait time seconds in the example above, we specify 10 seconds. Note that implicit and explicit wait for a clear distinction, implicit waits applied globally, whenever the driverdrive to find an element, implicitly waiting mechanism will be triggered (leading to slower test), if element is present, continue, otherwise, it will be round-robin fashion to determine whether the elements of successful positioning, wait until the timeout, throw an error NoSuchElementException. While waiting for explicit execution is specified only when certain (s) if there are elements.

Dormancy mechanism

Python use the sleep mechanism provided (that is, the time module), which is nothing to say, sleep would be finished, like where to where and so on and so on .......

import time
time.sleep(1)

In the choice of waiting mechanism, we have the flexibility to choose between three wait mechanisms:

  • Ordinary (static pages more) pages, waiting for sleep mechanisms and explicit mechanisms can be used with each other and improve efficiency.
  • More dynamic page when implicitly wait on rafts handy. Of course, implicit lazy rule on waiting. Once and for all .....

Guess you like

Origin www.cnblogs.com/Nayears/p/12177674.html