如何使用Selenium Webdriver区分同一页面上具有相同ID的两个元素? [英] How do I distinguish between two elements having same ids on same page using Selenium Webdriver?

查看:648
本文介绍了如何使用Selenium Webdriver区分同一页面上具有相同ID的两个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有两个列表,并且两个都有按钮(图像)以从列表中选择所有项目.这两个图像具有相同的ID,并且没有其他任何信息,例如类名,CSS,XPath.我想单击第二个图像以从第二个列表中选择所有项目.但是,当Web驱动程序执行以下行时:

I have two lists on my web page and both of them have button (image) to select all the items from the list. Both images have the same id and don't have any other information like class name, CSS, XPath. I want to click on the second image to select all the items from the second list. But when the web driver executes the following line:

driver.findElement(By.id("MoveAllRight")).click();

它总是单击第一个图像,但我希望它单击第二个图像.

It always clicks on the first image, but I want it to click on the second one.

有人可以帮我吗?

推荐答案

不要.相反,请提出一个错误.这是无效的HTML.

Don't. Instead, raise a bug. It's invalid HTML.

如果您真的想继续测试无效的东西,并且还想破坏自动化测试(您确实不想这样做),则可以执行以下操作:

If you really want to continue testing something that is invalid, and you also want to corrupt your automated tests (which you really don't want to do), you could do the following:

driver.findElement(By.xpath("(//img[@id='MoveAllRight'])[2]")).click();

它可以工作,但是这样做是错误的.

It will work, but it is the wrong thing to do.

这篇关于如何使用Selenium Webdriver区分同一页面上具有相同ID的两个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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