Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不等待30秒超时的情况下测试缺少元素_Python_Selenium_Webdriver - Fatal编程技术网

Python 如何在不等待30秒超时的情况下测试缺少元素

Python 如何在不等待30秒超时的情况下测试缺少元素,python,selenium,webdriver,Python,Selenium,Webdriver,我正在写一些功能测试,做一些简单的单页测试需要5分钟,因为当找不到元素时,find_元素函数需要30秒才能完成。我需要在不等待超时的情况下测试是否缺少元素。我一直在搜索,但到目前为止还没有找到任何替代方法来查找_element()。这是我的密码: def is_extjs_checkbox_selected_by_id(self, id): start_time = time.time() find_result = self.is_element_present(By.XPA

我正在写一些功能测试,做一些简单的单页测试需要5分钟,因为当找不到元素时,find_元素函数需要30秒才能完成。我需要在不等待超时的情况下测试是否缺少元素。我一直在搜索,但到目前为止还没有找到任何替代方法来查找_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

请注意,我没有接受前面的答案,因为以下海报的建议没有产生成功的结果。

我用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中的示例:)

我用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中的示例:)

给定您在问题中显示的代码,如果在Selenium确定缺少元素之前等待了30秒,这意味着您正在使用隐式等待。您应该停止使用隐式等待,而只使用显式等待。原因是迟早您会希望使用显式等待来精确控制Selenium等待的时间。不幸的是,隐式等待和显式等待并不混合。有关详细信息,请参阅

我使用两种通用方法来测试是否缺少元素,具体取决于条件。在使用Selenium测试动态应用程序时,我们面临的一个问题是:在哪一点上,您可以确定要检查其缺失的元素不会比您检查的那一刻晚几分之一秒出现?例如,如果您执行单击按钮的测试,它会启动一个Ajax请求,当请求失败时,可能会导致一个指示错误的元素添加到DOM中,并且在要求Selenium单击按钮后立即检查错误元素的存在,如果请求失败,则很可能会错过错误消息。您必须等待至少一点时间,才能让Ajax请求有机会完成。多长时间取决于你的申请

话虽如此,以下是我使用的两种方法

将缺席与出席配对 我将缺席测试与在场测试配对,使用
find_elements
而不是
find_element
,检查
find_elements
返回的数组上的长度是否为零

通过“我将缺席测试与在场测试配对”,我的意思是我识别了一个条件,比如页面上存在另一个元素。这个条件必须是这样的:如果条件为真,那么我不需要等待来测试缺席:我知道如果条件为真,我想测试缺席的元素必须最终缺席或出现在页面上。它不会在一秒钟后出现

例如,我有一个按钮,当单击该按钮时,它会通过Ajax调用执行检查,一旦Ajax调用完成,就会在页面上显示check complete,如果出现错误,则在下面添加带有错误消息的段落。为了测试没有错误的情况,我会等待出现
Check complete

,然后检查是否没有错误消息。我会使用
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\u引发的
只是可以使用的断言的一个示例。如果引发了
TimeoutException
,则断言将通过。

给定问题中显示的代码,如果在Selenium确定缺少元素之前等待了30秒,这意味着您正在使用隐式等待。您应该停止使用隐式等待,而只使用显式等待。原因是迟早您会希望使用显式等待来精确控制Selenium等待的时间。不幸的是,隐式等待和显式等待并不混合。有关详细信息,请参阅

我使用两种通用方法来测试是否缺少元素,具体取决于条件。在使用Selenium测试动态应用程序时,我们面临的一个问题是:在哪一点上,您可以确定要检查其缺失的元素不会比您检查的那一刻晚几分之一秒出现?例如,如果您执行单击按钮的测试,它会启动一个Ajax请求,当请求失败时,可能会导致一个指示错误的元素添加到DOM中,并且在要求Selenium单击按钮后立即检查错误元素的存在,如果请求失败,则很可能会错过错误消息。您必须等待至少一点时间,才能让Ajax请求有机会完成。多长时间取决于你的申请

话虽如此,这里有一个