简体   繁体   English

如何使用webdriver在行上查找具有动态ID的元素?

[英]How to find element with dynamic id on row using webdriver?

I have got html of element: 我有元素的html:

<li class="cabinet-module-holder cabinet-module-w4" ng-repeat="module in $ctrl.cabinetRow.modules track by module.id" dnd-draggable="module" dnd-effect-allowed="move" dnd-moved="$ctrl.removeModule($index, 1)" dnd-type="'0a2e00b3-8f89-4d17-a0c8-d37876043316'" dnd-dragstart="$ctrl.moduleDragStart({ cabinetModuleId: module.id })" dnd-disable-if="false" draggable="true">

I am using Webdriver framework in Visual Studio to move element on the rail/row. 我在Visual Studio中使用Webdriver框架在轨道/行上移动元素。 I want to valdiate that this element is on rail. 我想证明这个元素是正确的。 When the element is bigger cabinet module is w6 when it is small element is w2. 当元素较大时,机柜模块为w6;当元素较小时,机柜模块为w2。 The w4 is a medium size of each element. w4是每个元素的中等大小。

Now I have short C# code: 现在我有简短的C#代码:

string searchText = "0a2e00b3-8f89-4d17-a0c8-d37876043316";
                IList<IWebElement> rows = driver.FindElements(By.XPath("//*[@class='cabinet-body']"));
                foreach (IWebElement row in rows)
                {
                    if (row.Text.Contains(searchText))
                    {
                        SelectElement select = new SelectElement(row.FindElement(By.XPath("//*[@class='cabinet-module-holder cabinet-module-w6']")));
                        break;
                    }
                }

In the log output window I have empty place. 在日志输出窗口中,我有空的地方。 For that I use function logger.Write(); 为此,我使用功能logger.Write();

I want to validate element - it should be on correct rail, but the id is dynamic and after new browser or refresh is changing. 我想验证元素-它应该在正确的轨道上,但是ID是动态的,并且在更改新浏览器或刷新后。

Here is something I have used many times when working with dynamic elements. 在处理动态元素时,我已经使用了很多次。 It sounds like the id is more or less the same with the exception of w2 , w4 or w6 . 听起来好像id大致相同,但w2w4w6除外。 The following link taught me how to make advanced css selectors, which I have used to handle dynamic elements. 以下链接教我如何制作高级css选择器,该选择器用于处理动态元素。

https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/ https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/

For your given element you could write this many ways 对于给定的元素,您可以用多种方式编写

li[class^='cabinet-module-holder cabinet-module']

使用xpath

//*[contains(@class,'w4')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM