我是 selenium 的新手,我正在尝试编写一个代码,它将登录到我的网站并检查下拉菜单并单击按钮。 单击按钮后,将触发 http Angular 请求并显示结果。我的要求是等待响应并使用 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();
最佳答案
首先,让我们在启动驱动程序后添加一些隐式等待。现在这是 selenium 等待包括 angular 在内的任何元素的默认时间。
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
现在让我们考虑元素 A 在用户执行某些操作后出现。我的意思是元素 A 是有 Angular 的。现在检查元素是否存在。
driver.findElement(By.id(""));
//Perform the required operations after finding the element
脚本等待 Angular 元素出现 60 秒。如果现在我们将得到元素未找到异常。
其次,,我们可以利用显式等待来实现同样的目的。
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
或
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
希望对您有所帮助。谢谢。
关于java - 我需要添加一个等待,直到我得到来自带有 selenium-java 的 Angular http 请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44547560/