Merge two list with some conditional
Actually you are finding element from root means using //
in your xpath which will search for elements in the whole page while you need to search within the specific row element only so you should try with .//
in your xpath which will search for elements only specific element context. So I think you should try as below which will gives you only desire elements list instead of large amount of elements list as below :
ReadOnlyCollection<IWebElement> lstTable = browser.FindElements(By.XPath("//table/tbody/tr"));foreach (IWebElement val in lstTable){ ReadOnlyCollection<IWebElement> lstSpecialEle = val.FindElements(By.XPath(".//td/span | .//td/b | .//td/p"));}
Edited1 : If your getting list of elements with the combination of null text you can filter it with null condition and get sublists which contains exact text as below :-
var FinalList = lstSpecialEle.Where(x=>x.Text != null).ToList();
Edited2 :- if you want to merge all columns text list into Single list of string try as below :-
List<string> FinalList = new List <string>();foreach (IWebElement val in lstTable){ ReadOnlyCollection<IWebElement> lstSpecialEle = val.FindElements(By.XPath(".//td/span | .//td/b | .//td/p")); var AllTextList = lstSpecialEle.Where(x=>x.Text != null).ToList().Select(El => El.Text).ToList(); string AllText = String.Join(" ", AllTextList); FinalList.Add(AllText); }Console.WriteLine(FinalList);
Now FinalList
will contain all values separated by per row.
Hope it helps...:)