有没有办法让webDriverWait
等待多个元素之一出现,并根据哪个元素出现相应的行为?webdriver等待多个元素之一出现
此刻我在一个try循环中做了一个WebDriverWait
,如果发生超时异常,我运行等待其他元素出现的替代代码。这看起来很笨拙。有没有更好的办法?这里是我的(笨拙)代码:
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)
任何人有任何建议,以更有效的方式做到这一点?
谢谢。这会工作。 – amadain 2012-07-27 06:41:54