2014-09-28 75 views
4
<div id="loader-mid" style="position: absolute; top: 118.5px; left: 554px; display: none;"> 
    <div class="a">Loading</div> 
    <div class="b">please wait...</div> 
</div> 

並且想等到它消失。我有以下代碼,但它有時候等待時間過長,並且在某些代碼中,它突然凍結了所有進程,我不知道爲什麼。等到裝載機消失蟒蛇硒

from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.wait import WebDriverWait 

self.wait = WebDriverWait(driver, 10) 

self.wait.until(EC.invisibility_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]"))) 

,也是我這個嘗試之一:

self.wait.until_not(EC.presence_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]"))) 

我不知道如何檢查,但也許我的元素總是出現在頁面上,硒認爲它是存在的,唯一的參數顯示的變化從無變爲變化。我想我可以得到像字符串這樣的屬性,並檢查是否有單詞「block」,但它是如此錯誤我的事情......請幫助我。

+0

您是否嘗試檢查隱藏在加載器後面的元素的可見性? – ExperimentsWithCode 2014-09-29 20:02:47

+0

是的,它並沒有幫助我。但不知何故,現在第二個變種的作品奇怪。 – Michael 2014-09-30 20:52:08

+0

是的,這很奇怪。 – ExperimentsWithCode 2014-10-02 21:15:35

回答

2

重申你的答案(有一些錯誤處理),使人們更容易找到解決:)

最終變量

SHORT_TIMEOUT = 5 # give enough time for the loading element to appear 
LONG_TIMEOUT = 30 # give enough time for loading to finish 
LOADING_ELEMENT_XPATH = '//*[@id="xPath"]/xPath/To/The/Loading/Element' 

代碼別的地方PY文件

try: 
    # wait for loading element to appear 
    # : need to make sure we don't prematurely check if element 
    # : has disappeared before it has had a chance to appear 
    WebDriverWait(
      driver, SHORT_TIMEOUT 
    ).until(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH))) 

    # then wait for it to disappear 
    WebDriverWait(
      driver, LONG_TIMEOUT 
    ).until_not(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH))) 

except TimeoutException: 
    # if timeout exception was raised - should be safe to assume loading has finished. 
    # : However this may not always be the case, use with caution, othwise handle appropriately. 
    pass