ama*_*ain 10 python selenium webdriver wait selenium-webdriver
有没有办法让a webDriverWait等待一些元素出现,并根据出现的元素采取相应的行动?
目前我WebDriverWait在try循环中执行一个操作,如果发生超时异常,我会运行替代代码,等待另一个元素出现.这看起来很笨拙.有没有更好的办法?这是我(笨拙)的代码:
try:
    self.waitForElement("//a[contains(text(), '%s')]" % mime)
    do stuff ....
except TimeoutException:
    self.waitForElement("//li[contains(text(), 'That file already exists')]")
    do other stuff ...
它需要等待整整10秒才能查看该文件是否已存在于系统上的消息.
该函数waitForElement只执行了许多WebDriverWait调用:
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)
有人建议以更有效的方式完成这项工作吗?
创建一个函数,该函数将标识符映射到xpath查询并返回匹配的标识符.
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 ...
| 归档时间: | 
 | 
| 查看次数: | 5011 次 | 
| 最近记录: |