Selenium:如何定位通知消息等动态元素
我正在使用 Selenium 和 Web 驱动程序.
I am using Selenium with Web Driver.
我有一张表格需要在 Light Box 中填写.现在当我点击提交"时.该灯箱关闭,页面顶部生成一个简单的通知,该通知在几秒钟后消失.
I have a form to be filled up in a Light Box. Now when I click "submit". That Light Box gets closed and an easy notification gets generated on top of the page which gets disappeared after a couple of sec.
现在我的问题是:什么时候做
Now my question is: When I do
driver.findElement(By.xpath(".//*[@id='createCaseBtn']")).click(); // x-path of submit button
我应该如何检查该通知消息是否出现在 UI 上.
How shall I check whether that notification message appeared on the UI.
因为当我这样做
driver.findElement(By.xpath(".//*[@id='easyNotification']")).getText(); // x-path of easyNotification message
我告诉我无法找到逻辑上正确的元素,因为当时 UI 上不存在通知消息.只有在 AJAX 请求(用于提交表单)完成后,消息才会出现在 UI 上.
I shows me that it is Unable to locate the element which seems logically correct because at that time the notification message is not present on the UI. It is only after completion of AJAX Request (for submition of form) that message gets appeared on the UI.
请帮忙!!!!
谢谢
使用过显式等待.对我来说效果很好:
Have used Explicit Waits. It worked fine for me:
显式等待是您定义的代码,用于在进一步处理代码之前等待特定条件发生.最糟糕的情况是 Thread.sleep(),它将条件设置为要等待的确切时间段.提供了一些方便的方法来帮助您编写仅在需要时等待的代码.WebDriverWait 与 ExpectedCondition 相结合是实现此目的的一种方式.
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id(".//*[@id='easyNotification']")));