webdriver czeka na pojawienie się jednego z wielu elementów

Czy istnieje sposób na uzyskaniewebDriverWait poczekać, aż pojawi się jeden z wielu elementów i odpowiednio zareagować na podstawie tego, który element się pojawi?

W tej chwili robięWebDriverWait w pętli try i jeśli wystąpi wyjątek limitu czasu, uruchamiam alternatywny kod, który czeka na pojawienie się drugiego elementu. To wydaje się niezdarne. Czy jest lepszy sposób? Oto mój (niezdarny) kod:

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

Polega na odczekaniu przez całe 10 sekund, zanim zacznie sprawdzać, czy komunikat, że plik już istnieje w systemie.

FunkcjawaitForElement po prostu robi kilkaWebDriverWait połączenia w ten sposób:

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)

Czy ktoś ma jakieś sugestie, aby to osiągnąć w bardziej efektywny sposób?

questionAnswers(2)

yourAnswerToTheQuestion