基于Python實現(xiàn)的Selenium等待方法

Base on Python implementation

Scenarios

When a page is loaded by the browser, the elements within that page may load at different time intervals. This makes locating elements difficult: if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleExceptionexception. Using waits, we can solve this issue. Waiting provides some slack between actions performed - mostly locating an element or any other operation with the element.

Long story short

#1, wait until DOM is ready

#2, wait for complex page with JavaScript to load


Methods

Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait makes WebDriver wait for a certain condition to occur before proceeding further with execution. An implicit wait makes WebDriver poll the DOM for a certain amount of time when trying to locate an element.

Implicit Wait

Implicit Wait:implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.

Example

Explicit Wait + Expected Conditions

Explicit Wait:In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test. This provide you better option than implicit wait. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.ExpectedConditions class provide some set of predefine condition to wait element.


Example

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Expected Conditions

There are some common conditions that are frequent when automating web browsers. Listed below are Implementations of each. Selenium Python binding provides some convienence methods so you don’t have to code an expected_condition class yourself or create your own utility package for them

waits in Python

time.sleep()

Thread.sleep()- It will sleep time for script,not good way?to use in script as it's sleep without condition. What if 2 seconds are not enough in 5% of the cases?


Comparison

Here is a quick rundown on the differences between explicit and implicit wait:

Explicit wait:

- documented and defined behaviour.

- runs in the local part of selenium (in the language of your code).

- works on any condition you can think of.

- returns either success or timeout error.

- can define absence of element as success condition.

- can customize delay between retries and exceptions to ignore.

Implicit wait:

- undocumented and practically undefined behaviour.

- runs in the remote part of selenium (the part controlling the browser).

- only works on find element(s) methods.

- returns either element found or (after timeout) not found.

- if checking for absence of element must always wait until timeout.

- cannot be customized other than global timeout.


Problems

Combining implicit wait and explicit wait together results in unexpected wait times

Don't mix implicit and explicit waits. Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the WebDriver system. That means they're "baked in" to IEDriverServer.exe, chromedriver.exe, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile, and the Java remote WebDriver server (selenium-server-standalone.jar). Explicit waits are implemented exclusively in the "local" language bindings. Things get much more complicated when using RemoteWebDriver, because you could be using both the local and remote sides of the system multiple times

This is how that would work: local code -> Java remote server -> local Java language bindings on the remote server -> "remote" component like the Firefox extension, chromedriver.exe or IEDriverServer.exe. It's even more complex in the grid case, since there could be other hops in between.

Thus, when you try to mix implicit and explicit waits, you've strayed into "undefined behavior". You might be able to figure out what the rules of that behavior are, but they'll be subject to change as the implementation details of the drivers change. So don't do it.

You shouldn't be experiencing "hangs" when an element can't be found if you're not using implicit waits. The driver should throw a NoSuchElement exception immediately.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灸撰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子早芭,更是在濱河造成了極大的恐慌需纳,老刑警劉巖郭变,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡瑞躺,警方通過查閱死者的電腦和手機蚜印,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門莺禁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人窄赋,你說我怎么就攤上這事哟冬。” “怎么了忆绰?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵浩峡,是天一觀的道長。 經(jīng)常有香客問我错敢,道長翰灾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任稚茅,我火速辦了婚禮纸淮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘峰锁。我一直安慰自己萎馅,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布虹蒋。 她就那樣靜靜地躺著糜芳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪魄衅。 梳的紋絲不亂的頭發(fā)上峭竣,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天,我揣著相機與錄音晃虫,去河邊找鬼皆撩。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的扛吞。 我是一名探鬼主播呻惕,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼滥比!你這毒婦竟也來了亚脆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤盲泛,失蹤者是張志新(化名)和其女友劉穎濒持,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寺滚,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡柑营,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了村视。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片官套。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蓖议,靈堂內(nèi)的尸體忽然破棺而出虏杰,到底是詐尸還是另有隱情,我是刑警寧澤勒虾,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布纺阔,位于F島的核電站,受9級特大地震影響修然,放射性物質(zhì)發(fā)生泄漏笛钝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一愕宋、第九天 我趴在偏房一處隱蔽的房頂上張望玻靡。 院中可真熱鬧,春花似錦中贝、人聲如沸囤捻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝎土。三九已至,卻和暖如春绣否,著一層夾襖步出監(jiān)牢的瞬間誊涯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工蒜撮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留暴构,地道東北人。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像取逾,于是被迫代替她去往敵國和親耗绿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容