如何使用用于测试自动化的 JavaScript 查找具有相同属性的两个 Web 对象

编程入门 行业动态 更新时间:2024-10-28 13:23:52
本文介绍了如何使用用于测试自动化的 JavaScript 查找具有相同属性的两个 Web 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有这段 HTML 代码

I have this piece of HTML codes

我需要使用 JavaScript 找到这 2 个对象,以便我可以单击 () 它并编写一些文本(测试自动化).

I need to find those 2 objects using JavaScript so I can click() it and write some text (Test Automation).

问题是 id 是动态生成的,因此无法使用,因为它在每次页面加载时都在变化.现在因为无法使用 id ,这两个对象看起来相同,没有任何唯一标识符.

The issue is the id is dynamically generated, thus can't be used because it keeps changing everytime the page loads. Now because the id can't be used , those 2 objects looks the same without any unique identifier.

如果我用这个

document.querySelector("input.value-restored[type='text']");

然后它会找到第一个.

知道如何击中两个物体吗?

Any idea how can I hit both objects?

谢谢.

推荐答案

您可以使用 [contains()] 为这些元素构建 xpath.正如我所看到的,父 span 有一个您可以使用的稳定类名.

You can use [contains()] to construct a xpath for these elements. As I can see the parent span has a stable class name which you can use.

JAVA

WebElement firstElement = driver.findelements(By.xpath("//span[contains(@class,'form-row-ui-calender-row')]/input[@type='text']")).get(0);
WebElement secondElement = driver.findelements(By.xpath("//span[contains(@class,'form-row-ui-calender-row')]/input[@type='text']")).get(1);

或者在您的 xpath 中使用索引.

OR use indexing with your xpath.

WebElement firstElement = driver.findelements(By.xpath("//span[contains(@class,'form-row-ui-calender-row')]/input[@type='text'][1]"));
WebElement secondElement = driver.findelements(By.xpath("//span[contains(@class,'form-row-ui-calender-row')]/input[@type='text'][2]"));

希望这会有所帮助.

这篇关于如何使用用于测试自动化的 JavaScript 查找具有相同属性的两个 Web 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 09:18:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1132412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   属性   对象   两个   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!