2015-11-11 79 views
1

我有一個名爲webdriver.py的文件,它實現了selenium.webdriver庫中的方法。有同時處理最多的,我需要的情況下,等待功能:Python,Selenium - 處理多個等待條件

def wait_for(self, func, target=None, timeout=None, **kwargs): 
    timeout = timeout or self.timeout 
    try: 
     return WebDriverWait(self, timeout).until(func) 
    except TimeoutException: 
     if not target: 
      raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip()) 
     raise NoSuchElementException(target) 

func是一個選擇。

的問題是,有時DOM元素將是不可見的,從而引起異常,並檢測失敗。所以我想延長wait_for也等待元素變得可見。

喜歡的東西

def wait_for(self, func, target=None, timeout=None, **kwargs): 
    timeout = timeout or self.timeout 
    try: 
     return WebDriverWait(self, timeout).until(EC.element_to_be_clickable(func)).until(func) 
    except TimeoutException: 
     if not target: 
      raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip()) 
     raise NoSuchElementException(target) 

ECselenium.driver.expected_conditions

這當然不是工作 - 不支持任何until().until()語法..什麼事情發生,就像EC不存在。

任何想法?

回答

1

您可以使用EC.presence_of_element_located等待DOM中的元素可見,不需要第二個.until

def wait_for(self, func, target=None, timeout=self.timeout): 
    try: 
     WebDriverWait(self, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, func))) 
    except TimeoutException: 
     if not target: 
      raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip()) 
     raise NoSuchElementException(target) 
+0

感謝您的幫助,我還使用「從selenium.webdriver.support導入expected_conditions作爲EC」來解決EC問題。 – Oleg

+0

我也認爲它應該是:「By.ByCssSelector」根據https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.ByCssSelector.html – Oleg

+1

@ h3d0它應該是''By.CSS_SELECTOR爲Python檢查http://selenium-python.readthedocs.org/api.html?highlight=by.css_selector#selenium.webdriver.common.by.By.CSS_SELECTOR –