如何在不等待30秒超时的情况下测试元素不存在
我正在写一些功能测试,但进行一些简单的单页面测试时需要花费5分钟,因为当找不到元素时,find_element函数要花30秒才能完成。我需要测试某个元素是否不存在,而不想等到超时。我一直在寻找,但到目前为止还没有找到find_element()的替代方法。以下是我的代码:
def is_extjs_checkbox_selected_by_id(self, id):
start_time = time.time()
find_result = self.is_element_present(By.XPATH, "//*[@id='" + id + "'][contains(@class,'x-form-cb-checked')]") # This line is S-L-O-W
self.step(">>>>>> This took " + str( (time.time() - start_time) ) + " seconds")
return find_result
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
谢谢。
我按照这里和其他链接的大部分建议进行了尝试,但最终还是没能达到目标。当找不到元素时,它的表现和之前一样,还是要花30秒:
# Fail
def is_element_present_timeout(self, id_type, id_locator, secs_wait_before_testing):
start_time = time.time()
driver = self.driver
time.sleep(secs_wait_before_testing)
element_found = True
try:
element = WebDriverWait(driver, 0).until(
EC.presence_of_element_located((id_type, id_locator))
)
except:
element_found = False
elapsed_time = time.time() - start_time
self.step("elapsed time : " + str(elapsed_time))
return element_found
这是第二种方法,使用获取所有元素的思路。
# Fail
def is_element_present_now(self, id_type, id_locator):
driver = self.driver
# This line blocks for 30 seconds if the id_locator is not found, i.e. fail
els = driver.find_elements(By.ID, id_locator)
the_length = els.__len__()
if the_length == 0:
result = False
else:
result = True
self.step('length='+str(the_length))
return result
请注意,我已经取消接受之前的回答,因为按照发帖者的建议并没有得到成功的结果。
3 个回答
我写了一个简单的方法,用Java语言:
public boolean isElementPresent(long waitTime, String elementLocation) {
WebDriverWait wait = new WebDriverWait(driver, waitTime); // you can set the wait time in second
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementLocation)));
} catch (Exception e) {
return false;
}
return true;
}
(你可以在这里看到Python的例子: http://selenium-python.readthedocs.org/en/latest/waits.html)
根据你在问题中展示的代码,如果你在 Selenium 判断元素缺失之前有 30 秒的等待时间,这说明你在使用隐式等待。建议你停止使用隐式等待,改为只使用显式等待。原因是,迟早你会想要使用显式等待来精确控制 Selenium 等待的时间。不幸的是,隐式等待和显式等待是不能混用的。具体细节可以在 这个回答中找到。
我通常有两种方法来测试一个元素是否缺失,这取决于具体情况。使用 Selenium 测试动态应用时,我们面临的一个问题是:在什么情况下你能确定你想检查的元素不会在你检查的那一瞬间之后的短暂时间内出现呢?举个例子,如果你执行一个测试,点击一个按钮,这会发起一个 Ajax 请求,可能会在请求失败时在页面上添加一个表示错误的元素,而你在让 Selenium 点击按钮后立刻检查错误元素是否存在,那么如果请求失败,你很可能会错过这个错误信息。你需要等一等,给 Ajax 请求一个完成的机会。具体需要等多久取决于你的应用。
说到这里,我来介绍我使用的两种方法。
将缺失与存在结合起来
我将缺失测试和存在测试结合起来,使用 find_elements
而不是 find_element
,检查 find_elements
返回的数组长度是否为零。
这里的“将缺失测试和存在测试结合起来”是指我会识别页面上另一个元素的存在作为条件。如果这个条件成立,那么我就不需要等待来测试缺失:我知道如果这个条件成立,我想检查缺失的元素一定是缺失的,或者已经在页面上存在了。它不会在一瞬间之后才出现。
举个例子,我有一个按钮,点击后会通过 Ajax 调用进行检查,一旦 Ajax 调用完成,页面上会显示 <p>检查完成</p>
,如果有错误,还会在下面添加错误信息段落。为了测试没有错误的情况,我会等到 <p>检查完成</p>
出现,然后再检查错误信息是否缺失。我会使用 find_elements
来检查这些错误信息,如果返回的列表长度为 0,我就知道没有错误信息。检查这些错误信息本身不需要使用任何等待。
这种方法在没有 Ajax 的情况下也适用。例如,如果一个页面是由服务器生成的,不管是否有某个元素,那么就不需要等待,可以使用这里描述的方法。页面的“存在”(也就是说,页面已经加载完成)就是检查你想要确认缺失的元素的唯一条件。
等待超时
在我无法找到某个积极条件来与缺失检查配对的情况下,我会使用显式等待:
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from nose.tools import assert_raises
def test():
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".foo")))
assert_raises(TimeoutException, test)
在上面的代码中,driver
是之前创建的 Selenium 的 WebDriver
,timeout
是你希望的超时时间,assert_raises
只是一个可以使用的断言示例。如果抛出 TimeoutException
,那么断言就通过了。
经过一番研究,我发现自己一直没有找到解决办法,最后终于想出了一个可以接受的解决方案。其实这个问题的解决方法简单得让我有点不好意思。我只需要调整一下timeout(超时设置),使用implicitly_wait()这个函数就可以了,之前我不知道或者没注意到这个函数。我用的是Louis提供的语法,但其实只要在我最开始的is_element_present()函数前后加上driver.implicitly_wait()这个函数,做法会更简单,而且效果是一样的。
# Success
def is_element_present(self, id_type, id_locator):
driver = self.driver
element_found = True
driver.implicitly_wait(1)
try:
element = WebDriverWait(driver, 0).until(
EC.presence_of_element_located((id_type, id_locator))
)
except:
element_found = False
driver.implicitly_wait(30)
return element_found