有沒有辦法讓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