2015-11-11 65 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 –