2012-07-18 12 views
10

C'è un modo per ottenere un webDriverWait in attesa che uno di un numero di elementi appaia e di agire di conseguenza in base all'elemento che appare?webdriver attendere che compaia uno degli elementi multipli

Al momento eseguo un WebDriverWait all'interno di un ciclo di prova e se si verifica un'eccezione di timeout, eseguo il codice alternativo che attende che l'altro elemento venga visualizzato. Questo sembra maldestro. C'è un modo migliore? Qui è il mio codice (goffo):

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

Coinvolge in attesa di un intero 10 secondi prima che si cerca di vedere se il messaggio che il file è già presente sul sistema.

La funzione waitForElement solo che un certo numero di WebDriverWait chiamate in questo modo:

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) 

Qualcuno ha qualche suggerimento per realizzare questo in modo più efficiente?

risposta

7

creare una funzione che prende una mappa di identificatori di query XPath e restituisce l'identificatore che è stato abbinato.

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

grazie. Funzionerà. – amadain

1

Qualcosa del genere:

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 è un metodo selenio rc. La cosa principale qui è identificare quale dei percorsi 'o istruzione' è stato preso in modo che il valore restituito possa dire quale codice eseguire – amadain

Problemi correlati