Python selenium+webdriver three ways to wait time

Source: Internet
Author: User
Tags set time


Article Source: https://www.cnblogs.com/jshtest/p/6222923.html

In the process of automating development, sometimes in order to ensure the stability of the script, you need to add the wait time in the script.


Sleep (): Sets the fixed hibernation time. The Python time package provides hibernation (), and you can use sleep () to hibernate after the time package is imported.


Implicitly_wait (): Is a timeout waiting for Webdirver. Implicitly waiting for an element to be found, or a command to complete. Throws an exception if the setting time is exceeded.


Webdriverwait (): It is also the method provided by Webdirver. During the set time, the default detects whether the current page element exists at intervals, and throws an exception if the setting time is not detected.

code example:

#coding =utf-8
From selenium import Webdriver
#导入WebDriverWait Bag
From Selenium.webdriver.support.ui import webdriverwait
#导入time Bag
Import time

Driver = Webdriver. Firefox ()
Driver.get ("http://www.baidu.com")


#WebDriverWait () method uses the
Element=webdriverwait (Driver). Until (lambda driver:d river.find_element_by_id ("kw")
Element.send_keys ("Selenium")

#添加智能等待
Driver.implicitly_wait (30)
driver.find_element_by_id ("su"). Click ()

#添加固定休眠时间
Time.sleep (5)


Driver.quit ()

Knowledge Extension:

First, the lambda function

1, lambda function Base:

A lambda function is also called an anonymous function, that is, a function has no specific name, and a method created with Def has a name. As follows:

"" "named Foo function" "
def foo (): Return ' Beginman '  #Python中单行参数可以和标题写在一行
The Lambda keyword creates an anonymous function that is the same expression as the above function ""
Lambda: ' Beginman '             

The above is simply to create a function object with a lambda, without saving it or calling it, and the moment will be recycled. Here we save and invoke:

Bar = lambda: ' Beginman '
print bar ()      #beginman

The Python lambda syntax is easy to understand from the above examples:

Lambda [arg1[,arg2,arg3....argn]]:expression

In a lambda statement, a colon is a parameter and can have multiple, separated by commas, and the return value on the right side of the colon. A lambda statement is actually constructed of a function object.

Print lambda: ' Beginman '   #<function <lambda> at 0x00b00a30>

2, no parameters

If there are no arguments, the lambda Colon does not precede it, as in the example above.

3, has the parameter

def add (x,y): return x+y
add2 = lambda x,y:x+y
print add2 (1,2)     #3

def sum (x,y=10): return x+y
sum2 = Lambda x,y=10:x+y
Print sum2 (1)       #11
print sum2 (1,100) #101 1) sleep () The Sleep ()
method in seconds. If the sleep time is 1 seconds, it can be expressed in decimal.
import Time ...
.
Time.sleep (5)
time.sleep (0.5)
of course, you can also import the sleep () method directly to make the references in the script simpler from the time import sleep ...
. Sleep
(3)
(

2) implicitly_wait ()
implicitly_wait () method is more intelligent than sleep (), the latter can only choose a fixed time wait, The former can be intelligent in a time range waiting.

knowledge Extension:

the three kinds of waiting methods for Webdriver:

1. Mandatory Waiting

The first and most simple one way is to force waiting for sleep (xx), forced to let the flash, such as XX time, regardless of whether the concave-convex can keep up with speed, or has been early, must wait for xx time.

Look at the code:

1 2 3 4 5 6 7 8 #-*-coding:utf-8-*-from  Selenium import  Webdriver from  time import  sleep driver =  webdriver. Firefox () driver.get ('
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.