0

When I execute below command I'm getting more than 3000 items to my elm variable. Then I have to pass them in to a string array.

The below list contain so many blank data and there are some items which contains characters. But I need only elements with numbers. So is there any problematical way to remove all the blank elements and the elements with characters.

     IList<IWebElement> elm = driver.FindElements(By.ClassName("spy14"));

I was trying with a for each loop, but I stucked on some place.

EDIT:

    elm = elm.Where(x => !x.Text.Contains(" ")).ToArray();

With @PDHide's help I was able to get this thing done with above code.

  • Voting for closing because this question is regarding programming. On Stack Overflow people can tell you about list filters. – João Farias Aug 31 at 6:38
  • I was thinking IList<IWebElement> are mostly used by test engineers ,and that's why I post it here. – ChathuD Aug 31 at 8:50
3

You won't get blank data as its a IWebElement type list. If you meant that when you try to get text it returns empty for few elements then use:

 elm = elm.Where(x => !string.IsNullOrEmpty(x.Text)).ToList();

You will get elements for which Text is not Null or empty:

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.where?view=netcore-3.1#System_Linq_Enumerable_Where__1_System_Collections_Generic_IEnumerable___0__System_Func___0_System_Boolean__

For getting only numbers try

 elm = elm.Where(x => decimal.TryParse(x.Text, out decimal myInt)).ToList();
| improve this answer | |
  • Yeah its the text. – ChathuD Aug 31 at 8:46
  • This works fine to remove empty values.Thanks and I will mark this as a solution,But what about removing entries with string values? – ChathuD Aug 31 at 8:49
  • @ChathuD add filter for getting only numbers – PDHide Aug 31 at 9:11
  • That didnt work.Any way thanks for the support.Aftet the above code i was trying elm = elm.Where(x => !x.Text.Contains("s")).ToArray(); and this seems to be working. – ChathuD Aug 31 at 9:24
  • 1
    @ChathuD there was a extra semi colon you can try the new code – PDHide Aug 31 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.