Test if an element is focused using Selenium Webdriver(使用 Selenium Webdriver 测试元素是否聚焦)
问题描述
我真的很惊讶我在互联网上找不到使用 Selenium Webdriver 测试元素焦点的参考资料.
I'm really surprised I can't find references on the internet to testing for element focus using Selenium Webdriver.
我想检查何时尝试提交表单而遗漏了必填字段,焦点移至空字段.但我看不到任何使用 WebDriver API 的方法.
I'm wanting to check when when a form submission is attempted with a mandatory field missed, focus is moved to the empty field. But I cannot see any way to do this using the WebDriver API.
我将能够使用 JavascriptExecutor.但是阅读 FAQ 让我觉得一定有某种方法可以使用驱动程序本身来执行检查.
I will be able to find the focused element using a JavascriptExecutor. But reading the FAQ makes me think there must be some way to perform the check using the driver itself.
感谢您的帮助.
推荐答案
driver.switchTo().activeElement() 将返回当前聚焦的WebElement.WebElement 已经很好地定义了相等性,因此您可以调用 element.equals(driver.switchTo().activeElement()).
driver.switchTo().activeElement() will return the currently focused WebElement. Equality is well defined for WebElement, so you can call element.equals(driver.switchTo().activeElement()).
调用有点误导性的名为driver.switchTo().activeElement()实际上并没有切换焦点,driver.findElement()也没有,所以你没有需要在 switchTo().defaultContent() 之后;事实上,这样做可能会模糊当前元素.
Calling the slightly misleading named driver.switchTo().activeElement() does not in fact switch focus, neither does driver.findElement(), so you do not need to switchTo().defaultContent() after; in fact, doing so would probably blur the current element.
这篇关于使用 Selenium Webdriver 测试元素是否聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Selenium Webdriver 测试元素是否聚焦
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- 如果条件可以为空 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
