在包含多个元素的页面中按类名称选择元素-Selenium和C# [英] Select element by Class Name in a page with multiple elements - Selenium and C#

查看:202
本文介绍了在包含多个元素的页面中按类名称选择元素-Selenium和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver + C#来自动化报告中的测试.该报告具有一个具有多列和多行的表,并且该表中的每个单元格都有由名称和唯一标识符组成的类名(此标识符每次生成报告时都会更改).示例:

I 'm using Selenium WebDriver + C # to automate a test in a report. This report has a table with multiple columns and rows and each cell in this table has the class name composed by a name and a unique identifier (this identifier changes every time the report is generated ). Example:

< div title =" class ="colSerialNumber s16-115435-ec6e115435-96ee" ...

< div title = " " class = " colSerialNumber s16-115435 - ec6e115435-96ee " ...

因此,表中的每一行都有一个单元格"colSerialNumber". 我的问题是:有没有一种方法可以找到硒中的每个"colSerialNumber",并按类名"进行选择?

So, every row in the table has a cell "colSerialNumber". My question is: Is there a way to find each "colSerialNumber" in selenium, selecting for its ocurrence by Class Name?

示例:

FindElement(By.ClassName(colSerialNumber(FirstOcurrence)))

FindElement(By.ClassName(colSerialNumber(FirstOcurrence)))

FindElement(By.ClassName(colSerialNumber(SecondOcurrence)))

FindElement(By.ClassName(colSerialNumber(SecondOcurrence)))

(我知道语法是错误的,这只是一个主意...)

(I know the syntax is wrong, it's just an idea ...)

类似于CSS选择器使用的nth-child.

Something like nth-child used with CSS Selector.

有什么主意吗?

推荐答案

请注意,自从我使用C#已有一段时间了.如果您致电:

Note it has been some time since I used C#. If you call:

FindElements(By.ClassName("colSerialNumber"))

注意复数的元素.这应该为您提供所有匹配项的列表.然后,您可以使用列表索引找到所需的内容:1st,2nd,...,nth.

Note the plural Elements. This should give you a list of all matches. You can then use list indexes to find what you want: 1st, 2nd, ..., nth.

这篇关于在包含多个元素的页面中按类名称选择元素-Selenium和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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