简体   繁体   English

如何通过非null属性和ID查找元素?

[英]How to find an element by a not null attribute and ID?

I have two buttons in a page/form. 我在页面/表单中有两个按钮。

They both have the class name of "button add regulation" 它们都具有“按钮添加规则”的类名称。

One of them always has an attribute of "deviceid" that has some value, and one of them never does. 其中之一始终具有“ deviceid”的属性,该属性具有一定的值,而其中之一则从未如此。

When I refer to these button within my test script, i tried using this CSS selector 当我在测试脚本中引用这些按钮时,我尝试使用此CSS选择器

FindElement(By.CssSelector(".button.add.regulation")).Click();  

which works when attempting to click the first button, which does not have the second attribute. 尝试单击不具有第二个属性的第一个按钮时,该按钮有效。

But when the test script hits that same snippet of code when referring to the 2nd button (which has the same class name PLUS a deviceID attribute) it fails because the element is not visible. 但是,当测试脚本引用第二个按钮(具有相同的类名以及deviceID属性)时命中相同的代码片段时,它将失败,因为该元素不可见。

How do we reference that second button that shares a class name with another button, but has a secondary attribute of "deviceid"? 我们如何引用与另一个按钮共享类名但具有次要属性“ deviceid”的第二个按钮?

Try below code and let me know the result: 尝试下面的代码,让我知道结果:

FindElement(By.XPath("//button[@class='button add regulation' and @deviceid]")).Click();  

This should match button with deviceid attribute 这应该与具有deviceid属性的按钮匹配

If both buttons have deviceid attribute while only one of them has not empty deviceid : 如果两个按钮都具有deviceid属性,而只有一个按钮不具有空的deviceid

FindElement(By.XPath("//button[@class='button add regulation' and string-length(@deviceid)>0]")).Click(); 

Proper implementation. 正确执行。

Please note usage of different variable names as compared to the original post/question. 请注意,与原始帖子/问题相比,使用了不同的变量名。

Given: 鉴于:

 parentRow = applicationNumberLinks[rowIndex].FindElement(By.XPath("../../../.")); 
 //ugly i know just bear with me

if u want to find the TDs within that row that have a class where the name is greater than 0: 如果您想在该行中查找具有名称大于0的类的TD:

 cellsInRow = parentRow.FindElements(By.XPath(".//td[string-length(@class)>0]"));

At least, that is what is working for what I need. 至少,这正是我需要的。 Please experiment and modify to your specific use case 请尝试并修改您的特定用例

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

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