Java Wait for a HTML element and record the mouse click through WebDriverEventListener(Java 等待一个 HTML 元素并通过 WebDriverEventListener 记录鼠标点击)
问题描述
我正在开发一个 java 应用程序来帮助构建 selenium 测试,我想知道是否可以强制应用程序等待单击,然后单击确定单击了 html 的哪个元素
I am developing a java application to help the construction of selenium tests and I would like to know if it is possible to force the application to wait for a click and after that click identify which element of html was clicked
问候
推荐答案
回答你的问题:
如果可以强制应用程序等待点击:从技术上讲,click()
的调用由终端用户管理,终端用户也是脚本/程序.再次功能上,您的脚本/程序不需要等待 click()
而是需要等待预期的 WebElement 成为 interactable(即 可点击).与此用例类似,当您自动化测试用例时,您可能必须将快速移动的 WebDriver 实例与滞后的 Web 客户端 同步.为了实现这一点,Selenium 为您提供了 WebDriverWait Class 可以与 ExpectedConditions 类.
If it is possible to force the application to wait for a click : Technically the invokation of click()
is governed by the enduser who is also the owner of the script/program. Again functionally your script/program need not wait for click()
but need to wait for the intended WebElement to be interactable (i.e. clickable). Similar to this usecase while you automate your testcases you may have to synchronize the fast moving WebDriver instance with the lagging Web Client. To achieve that Selenium provides you the WebDriverWait Class which can be used in conjunction with ExpectedConditions Class.
预期条件 Class 使我们能够遵守许多条件.几个最广泛使用的ExpectedConditions如下:
ExpectedConditions Class enables us to comply with numerous conditions. A couple of most widely used ExpectedConditions are as follows :
- presenceOfElementLocated(按定位器)
- visibilityOfElementLocated(按定位器)
- elementToBeClickable(按定位器)
- frameToBeAvailableAndSwitchToIt(By locator)
- numberOfwindowsToBe(int expectedNumberOfWindows)
点击之后确定点击了哪个 html 元素:要实现这一点,您必须借助 EventFiringWebDriver 将注册一个 EventHandler 实例,该实例将实现 WebDriverEventListener
After that click identify which element of html was clicked : To acieve this you have to take help of EventFiringWebDriver which will register an instance of EventHandler which will implement WebDriverEventListener
EventFiringWebDriver 是对任意 WebDriver 实例的封装,它支持注册 WebDriverEventListener,主要用于日志记录.
EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance which supports registering of a WebDriverEventListener majorly for logging purposes.
EventFiringWebDriver程序示例:
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
EventHandler handler = new EventHandler();
eventDriver.register(handler);
eventDriver.get("https://google.com");
System.out.println(eventDriver.getTitle());
EventHandler类的一个例子:
public class EventHandler implements WebDriverEventListener
{
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Inside the afterNavigateTo to " + arg0);
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Just before beforeNavigateTo " + arg0);
}
}
控制台输出:
Just before beforeNavigateTo https://google.com
Inside the afterNavigateTo to https://google.com
Google
这篇关于Java 等待一个 HTML 元素并通过 WebDriverEventListener 记录鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 等待一个 HTML 元素并通过 WebDriverEventListener 记录鼠标点击
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01