简体   繁体   中英

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.

When I refer to these button within my test script, i tried using this CSS selector

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.

How do we reference that second button that shares a class name with another button, but has a secondary attribute of "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

If both buttons have deviceid attribute while only one of them has not empty 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:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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