首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium XHR驱动程序等待后台XHR请求

Selenium XHR驱动程序等待后台XHR请求
EN

Stack Overflow用户
提问于 2020-03-03 16:51:14
回答 1查看 199关注 0票数 0

我试着用Selenium在Salesforce上实现自动化。如何等待搜索结果显示在屏幕截图中?方法-1还是方法-2?

代码语言:javascript
复制
//Method-1
public void waitForPageLoadXHR(WebDriver driver) {
    new WebDriverWait(driver, 15).until(
            driver1 -> ((JavascriptExecutor) driver1).executeScript("return document.readyState").equals("complete"));
}

//Method-2
    public void waitForPageLoadXHR(WebDriver driver) {
        new WebDriverWait(driver, 15).until(
                driver1 -> ((JavascriptExecutor) driver1).executeScript("return jQuery.active == 0"));
    }

更新:这里是的一个表示。很抱歉,由于遵从性问题,我无法发布实际的HTML。< and >是我的文本框,搜索结果是< li >s‘。< li >以我键入的方式动态显示。

EN

回答 1

Stack Overflow用户

发布于 2020-04-01 17:40:19

(1)我看到您正在搜索一个帐户,并在Salesforce页面的全局搜索字段上发送字符串。我这里的第一个建议是‘不要复制和粘贴’您的帐户(或任何名称)直接在这里。Salesforce照明是一个沉重的场所,比通常的地点花费更多的时间。所以你能做的是-一个字一个字地给你,少等。我知道这个技巧,你可能不喜欢,但它对我是100% -见下面的代码:

代码语言:javascript
复制
for (int i = 0; i < value.length(); i++){
   char character = value.charAt(i);
    String inputCharacterText = new  StringBuilder().append(character).toString();                  
    driver.findElement(webElement).sendKeys(inputCharacterText);
    Thread.sleep(50);
    }  

(2)你主要问题的解决办法:

TestWait.waitForElementToBeVisible(驱动程序,PageClass.searchAccount(驱动程序));

这是您的WebElement:(由于您正在搜索特定的帐户,然后尝试使用使用xpath的自定义文本-example“//*@-example=‘AccountName’”来使用-example)

代码语言:javascript
复制
public static By  searchAccount= By.xpath("You XPATH go here ");

public static By searchAccount(WebDriver driver) {

    return searchAccount ;
}

请使用下面提到的等待方法,它对我来说非常有用:

类是"TestWait“,您在类中定义了下面的方法

公共静态布尔waitForElementToBeVisible(WebDriver驱动程序,由expectedElement提供){

代码语言:javascript
复制
    boolean webElement = false;
    logger.info("Executing waitForElementToBeVisible for page element");
    try {
        logger.info("Waiting for web element " + expectedElement + " with fluent wait");

          driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
          Wait<WebDriver> wait = new
          FluentWait<WebDriver>(driver)
          .withTimeout(Duration.ofSeconds(45))
          .pollingEvery(Duration.ofMillis(200))
          .ignoring(org.openqa.selenium.NoSuchElementException.class,org.openqa.
          selenium.StaleElementReferenceException.class)
          .ignoring(org.openqa.selenium.ElementNotVisibleException.class);

          wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));    

        webElement = true;
    } catch (Exception e) {
        logger.error(expectedElement + " : Not found the element on Page");
        webElement = false;
        Assert.fail("WebElement is not visible");
    } finally {
        driver.manage().timeouts().implicitlyWait(TestConstant.ImplicitWaitTime, TimeUnit.SECONDS);
    }
    return webElement;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60511993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档