如何使用用于测试自动化的 JavaScript 查找具有相同属性的两个 Web 对象 [英] How to find two web objects with the same properties using JavaScript for Test Automation

查看:23
本文介绍了如何使用用于测试自动化的 JavaScript 查找具有相同属性的两个 Web 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆