webdriver wait for one of a multiple elements to appear

后端 未结 2 1160
醉梦人生
醉梦人生 2021-02-14 16:17

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

2条回答
  •  时光说笑
    2021-02-14 17:00

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

提交回复
热议问题