webdriver wait for one of a multiple elements to appear

﹥>﹥吖頭↗ 提交于 2019-12-09 09:51:51

问题


Is there a way to get a webDriverWait to wait for one of a number of elements to appear and to act accordingly based on which element appears?

At the moment I do a WebDriverWait within a try loop and if a timeout exception occurs I run the alternative code which waits for the other element to appear. This seems clumsy. Is there a better way? Here is my (clumsy) code:

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

It involves waiting an entire 10 seconds before it looks to see if the message that the file already exists on the system.

The function waitForElement just does a number of WebDriverWait calls like so:

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)

Anybody got any suggestions to accomplish this in a more efficient way?


回答1:


Create a function that takes a map of identifiers to xpath queries and returns the identifier that was matched.

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 ...



回答2:


Something like that:

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


来源:https://stackoverflow.com/questions/11539930/webdriver-wait-for-one-of-a-multiple-elements-to-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!