Java 等待一个 HTML 元素并通过 WebDriverEventListener 记录鼠标点击

Java Wait for a HTML element and record the mouse click through WebDriverEventListener(Java 等待一个 HTML 元素并通过 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 记录鼠标点击

基础教程推荐