有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

angularjs我需要添加一个等待,直到我通过seleniumjava收到AngularHTTP请求的响应

我对selenium非常陌生,我正在尝试编写一个代码,该代码将登录到我的网站,检查下拉列表并单击按钮。 单击按钮后,将触发http角度请求并显示结果。我的要求是等待响应并使用selenium检查响应,然后从我的网站注销

    driver = new ChromeDriver();

    driver.get("https://url");
    driver.findElement(By.id("userId")).sendKeys("USERID");
    driver.findElement(By.id("password")).sendKeys("PASSWORD");
    driver.findElement(By.id("Submit")).click();

    Select dropdownA = new Select(driver.findElement(By.id("mySelectA")));
    dropdownA.selectByValue("2");
    Select dropdownB = new Select(driver.findElement(By.id("mySelectB")));
    dropdownB.selectByValue("5");

    driver.findElement(By.id("findroute")).click();

    /***/Here i need to wait for the angular http request to reply and check for the data displayed***

    driver.findElement(By.id("userTemp")).click();
    driver.findElement(By.linkText("Logout")).click();
    driver.close();

共 (1) 个答案

  1. # 1 楼答案

    首先,让我们在启动驱动程序后添加一些隐式等待。现在这是selenium等待任何元素(包括angular)的默认时间

    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    

    现在让我们考虑元素A在用户执行的一些动作之后出现。元素A是有角度的。现在检查元素是否存在

    driver.findElement(By.id(""));  
    //Perform the required operations  after finding the element
    

    脚本等待60秒,以显示角度元素。如果现在我们将得到元素未找到异常

    其次,,我们可以利用显式等待来实现同样的目标

    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
    

    或者

    wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
    

    希望这对你有帮助。谢谢