首页 > 解决方案 > 等待元素显示在 Web 驱动程序采样器中不起作用

问题描述

我正在使用 webdriver 采样器进行测试,我正在等待一个表格出现在我的页面上,但该表格不起作用,

var driver = JavaImporter(org.openqa.selenium); //WebDriver classes
var driver_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver classes
var start = new Date().getTime()

WDS.sampleResult.sampleStart();
WDS.sampleResult.getLatency();
WDS.browser.get("https://<some_url>/mrlb/f4d7f910-03bf-4c6f-93aa-0d47d7ec5afd");
var attempt = 1 ;

while (new Date().getTime() - start < 5000) {
    try{
        if (WDS.browser.findElement(driver.By.className('.dataTables_wrapper.form-inline.dt-bootstrap.no-footer')).isDisplayed()) {
        WDS.log.info('Table is displayed')
        break;
    } 
} 
    catch(err){
        if (attempt < 10) {
            WDS.log.info('Attempt # ' + attempt + ', Element not found');
            java.lang.Thread.sleep(2000)
            attempt++
            WDS.log.info('Current Value of Attemp # ' + attempt);
        }          
}
}

WDS.sampleResult.sampleEnd();

也试过

wait.until(driver.ExpectedConditions.elementToBeClickable(pkg.By.className('.dataTables_wrapper.form-inline.dt-bootstrap.no-footer')));
        WDS.log.info("Table displayed");
        break;

还检查了css定位器很好,

CSS 定位器

标签: jmeter

解决方案


你能试试这个吗

var driver = JavaImporter(org.openqa.selenium); //WebDriver classes
var driver_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver classes
var start = new Date().getTime()
var conditions = org.openqa.selenium.support.ui.ExpectedConditions
var wait=new driver_ui.WebDriverWait(WDS.browser, 20)
var exception = null

WDS.sampleResult.sampleStart();
WDS.sampleResult.getLatency();
WDS.browser.get("https://<url>");
try {
    wait.until(conditions.presenceOfElementLocated(driver.By.className('.dataTables_wrapper.form-inline.dt-bootstrap.no-footer')))
    WDS.sampleResult.setSuccessful(true);
}

catch (error){
    exception = error;
    WDS.sampleResult.setSuccessful(false);
}
WDS.sampleResult.sampleEnd();

参考:要遵循的 Web 驱动程序规则


推荐阅读