2012-07-18 19 views
10

¿Hay alguna manera de obtener un webDriverWait para esperar la aparición de uno de los elementos y actuar según el elemento?webdriver esperar a que aparezca uno de los elementos múltiples

Por el momento hago un WebDriverWait dentro de un ciclo de prueba y si ocurre una excepción de tiempo de espera ejecuto el código alternativo que espera a que aparezca el otro elemento. Esto parece torpe ¿Hay una mejor manera? Aquí está mi (torpe) Código:

try: 
    self.waitForElement("//a[contains(text(), '%s')]" % mime) 
    do stuff .... 
except TimeoutException: 
    self.waitForElement("//li[contains(text(), 'That file already exists')]") 
    do other stuff ... 

Se trata de esperar todo un 10 segundos antes de que se mira para ver si el mensaje que el archivo ya existe en el sistema.

La función waitForElement solo que una serie de WebDriverWait llamadas así:

def waitForElement(self, xPathLocator, untilElementAppears=True): 
    self.log.debug("Waiting for element located by:\n%s\nwhen untilElementAppears is set to %s" % (xPathLocator,untilElementAppears)) 
    if untilElementAppears: 
     if xPathLocator.startswith("//title"): 
      WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator)) 
     else: 
      WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator).is_displayed()) 
    else: 
     WebDriverWait(self.driver, 10).until(lambda driver : len(self.driver.find_elements_by_xpath(xPathLocator))==0) 

Alguien tiene alguna sugerencia para lograr esto de una manera más eficiente?

Respuesta

7

Crear una función que toma un mapa de identificadores de consultas XPath y devuelve el identificador que fue igualada.

def wait_for_one(self, elements): 
    self.waitForElement("|".join(elements.values()) 
    for (key, value) in elements.iteritems(): 
     try: 
      self.driver.find_element_by_xpath(value) 
     except NoSuchElementException: 
      pass 
     else: 
      return key 

def othermethod(self): 

    found = self.wait_for_one({ 
     "mime": "//a[contains(text(), '%s')]", 
     "exists_error": "//li[contains(text(), 'That file already exists')]" 
    }) 

    if found == 'mime': 
     do stuff ... 
    elif found == 'exists_error': 
     do other stuff ... 
+0

gracias. Eso funcionará – amadain

1

Algo así:

def wait_for_one(self, xpath0, xpath1): 
    self.waitForElement("%s|%s" % (xpath0, xpath1)) 
    return int(self.selenium.is_element_present(xpath1)) 
+0

is_element_present es un método selenium rc. Lo principal aquí es identificar cuál de las rutas 'o declaración' se tomó para que el valor devuelto pueda decir qué código ejecutar – amadain

Cuestiones relacionadas