Must be interpreted with selenium to wait for the three waiting mode - [turn] Python selenium

Reprinted from: https: //blog.csdn.net/huilan_same/article/details/52544521 invasion deleted

 

Found that too many people will not use wait, bloggers today really could not help but give us something about the necessity of waiting.

A lot of people in the group asked, not positioning this drop-down box, pop-up box that is positioned less than ... various positioning not, in fact, in most cases is that both of these problems: There are 1 frame, 2 plus no waiting. As everyone knows, your code speed is what order, and the browser loads rendering speed is on the order of what, like Dwyane Wade and bump Man made an appointment to play monsters, then ask bump Man you kick back after Dwyane Wade why also did not wear shoes to go out? Bump Man sub-divided in heart ten thousand alpacas flying, slow bullying brother, brother do not play with you, throw exceptions refuse to work up.

So how can we take care of the bump Man slow loading speed? There is only one way, that is myself and so on. Speaking of, etc., there are three other laws, hear bloggers one by one:

1. forced to wait

The first one is the most simple and crude way is forced to wait for sleep (xx), xx etc. to force Dwyane time, regardless of the bump Man can not keep up with the speed, or has been brought forward to, and so must xx time.

Look at the code:

 

 

This is called forced to wait, no matter whether you load the browser finished, the program had to wait for three seconds, three seconds is up, continue with the following code as useful for debugging, so sometimes you can wait in the code, but is not recommended In this way the total waiting, too rigid, serious impact on program execution speed.

2. implicit wait

The second approach is called hidden wait, implicitly_wait (xx), waiting for the hidden meaning is: bump Man Dwyane Wade and a good agreement, regardless of where Dwyane go bump Man have to wait xx seconds at this time if the bump Man come inside, then both men immediately set out to fight the monster, if the bump Man within the specified time not to, then Dwyane themselves, it is naturally waiting to give you bump Man Throws it.

Look at the code:

 

 

Stealth is set to wait for a maximum waiting time, if the page load completed within the stipulated time, then the next step, otherwise wait until the deadline, then the next step. Note that there is a downside, that program will wait until the entire page load is complete, that is, under normal circumstances you see the browser tab bar that no longer turn a small circle, the next step will be, but sometimes you want a page element early in loaded, but because individual things like js particularly slow, I still have to wait until page fully completed before the next step, I want to wait after the elements I want to come out and how to do next? There are ways, which is another way to look at selenium waiting to offer - the dominant wait wait.

Of particular note are: hidden waiting period for the entire driver have to work, so long as the set once, I have seen people waiting as a recessive sleep with, have to look at where to go ...

3. dominant waiting

A third way is to wait for explicit, WebDriverWait, until the class with () and until_not () method, can be performed flexibly according to the judgment wait condition. It mainly means: xx seconds to look at every program, if the conditions are established, the next step, otherwise continue to wait until the maximum time expires, then throw TimeoutException.

Look at the code sample:

 

 

In the above example, we set the implicit and explicit wait wait, in other operations, the hidden waiting play a decisive role in WebDriverWait .. waiting for the dominant play a major role, but it should be noted that: the longest waiting time between the two depends on the larger, 20 in this example, if the waiting time recessive> explicit wait time, the maximum waiting time is equal to the sentence code recessive latency.

We mainly used the WebDriverWait class and expected_conditions module, the following bloggers bring you a closer look at these two modules:

WebDriverWait

WebDriverWait dominant class wait wait class module, which look under it and method parameters:

selenium.webdriver.support.wait.WebDriverWait(类)

__init__

driver: Incoming WebDriver instance, that our example of the driver

timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)

poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒

ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,

则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。

until

method: 在等待期间,每隔一段时间(__init__中的poll_frequency)调用这个传入的方法,直到返回值不是False

message: 如果超时,抛出TimeoutException,将message传入异常

until_not

与until相反,until是当某元素出现或什么条件成立则继续执行,

until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。

看了以上内容基本上很清楚了,调用方法如下:

WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)

这里需要特别注意的是until或until_not中的可执行方法method参数,很多人传入了WebElement对象,如下:

WebDriverWait(driver,10).until(driver.find_element_by_id('kw'))# 错误

这是错误的用法,这里的参数一定要是可以调用的,即这个对象一定有__call__()方法,否则会抛出异常:

TypeError:'xxx'objectisnot callable

在这里,你可以用selenium提供的expected_conditions模块中的各种条件,也可以用WebElement的is_displayed()、is_enabled()、is_selected()方法,或者用自己封装的方法都可以,那么接下来我们看一下selenium提供的条件有哪些:

expected_conditions

expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件:

selenium.webdriver.support.expected_conditions(模块)

以下两个条件类验证title,验证传入的参数title是否等于或包含于driver.title

title_is

title_contains

以下两个条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, ‘kw’)

顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行

presence_of_element_located

presence_of_all_elements_located

以下三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement

第一个和第三个其实质是一样的

visibility_of_element_located

invisibility_of_element_located

visibility_of

以下两个条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value

text_to_be_present_in_element

text_to_be_present_in_element_value

以下条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement

frame_to_be_available_and_switch_to_it

以下条件判断是否有alert出现

alert_is_present

以下条件判断元素是否可点击,传入locator

element_to_be_clickable

以下四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组

第三个传入WebElement对象以及状态,相等返回True,否则返回False

第四个传入locator以及状态,相等返回True,否则返回False

element_to_be_selected

element_located_to_be_selected

element_selection_state_to_be

element_located_selection_state_to_be

最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了

staleness_of

上面是所有17个condition,与until、until_not组合能够实现很多判断,如果能自己灵活封装,将会大大提高脚本的稳定性。

Guess you like

Origin www.cnblogs.com/wilson-5133/p/11118748.html