selenium - 如何使用包装元素初始化 WebElement
问题描述
在执行任何操作之前,我已遵循有关如何关闭随机打开的弹出窗口的教程:
https://www.vinsguru.com/selenium-webdriver-how-to-handle-annoying-random-popup-alerts/
这个想法是创建一个实现接口 InvocationHandler 的 ElementProxy 类。因此,代理的调用方法将在调用实际方法之前首先被调用。
因此,我们在对 WebElement 调用任何操作之前调用 checkForPopupAndKill。
然后我们用这个代理对象包装我们的常规 WebElement。我们基本上需要一个类,它有一个方法来接受 WebElement 并返回带有一些包装器的 WebElement。
在本教程中,他们使用包装器元素初始化页面对象的元素,如下所示:
//first init elements
PageFactory.initElements(driver, pageobject);
//then access all the WebElements and create a wrapper
for(Field f:pageobject.getClass().getDeclaredFields()){
if(f.getType().equals(WebElement.class)){
boolean accessible = f.isAccessible();
f.setAccessible(true);
//reset the webelement with proxy object
f.set(pageobject, ElementGuard.guard((WebElement) f.get(pageobject)));
f.setAccessible(accessible);
}
}
那是因为他们已经在页面对象中声明了带有@FindBy
注释的文件,但是在我的框架中,我将元素声明如下:
WebElement elt = getSmartElement(By.cssSelector("#my_id"));
我的问题是,如何使用包装元素初始化我的元素?
提前致谢。
解决方案
您可以直接使用 ElementGuard,如此处所示,它使用代理包装实际的 WebElement。
WebElement elt = ElementGuard.guard(getSmartElement(By.cssSelector("#my_id")));
推荐阅读
- javascript - 游戏循环中一个元素上的两个 jQuery 动画
- reactjs - 如何在 react.js 的新选项卡中打开组件?
- jsp - JSP 未在 Spring Boot 中呈现
- compression - 压缩大的、几乎相同的文件
- http2 - 为什么在 light-4j 中同时启用 https/http2 时端口 8080 停止工作
- java - 使用 Zenmuse XT2 将相机模式更改为视频失败并显示错误:不支持
- vuejs2 - VueJS试图在父组件处理输入之前捕获子组件上的输入
- python - 在python中减少数据框的维度
- sql - 如何选择一个更新的sql,以便获取新更新的数据
- sql-server - 带有反向 POCO 生成器的实体框架中存储过程的返回值问题