I have a table in which the rows are shuffled every time. I want to select a particular row by its text, which I'm not able to do that. If I use xpath or cssSelector I am able to select by its position but not by name. Can you help me out to select this?

  • 2
    What do you mean "row by its text"? Normally rows do not contain text, but the columns. Give us the example of your table.
    – Alexey R.
    Mar 7, 2018 at 12:26
  • It means - by the name it appears on the UI page.
    – Premaja
    Mar 7, 2018 at 12:39
  • I've done this by text using XPATH, but it's hard to know what this would look like for you without seeing some of the html for what you're trying to select. Can you include that in the question? I prefer css selectors, but if you have to search by text and go back up the dom xpath is the way to go.
    – mrfreester
    Mar 7, 2018 at 16:11
  • If you want the tr, it might look something like this... //tr[.//*[contains(text(),'Your text')]. It's hard to say for sure without seeing the html or which element exactly you're trying to click.
    – mrfreester
    Mar 7, 2018 at 17:48
  • My url is not working currently so I'm unable to send the html code and unable to test it .I will let you know if it works.Sorry for delayed response
    – Premaja
    Mar 9, 2018 at 5:15

1 Answer 1

string mystring;
IList<IWebElement> mylist;
//Search Table
mylist = driver.FindElements(By.XPath(Table));
if (mylist.Count >= 1)
    foreach (IWebElement ele in mylist)
        mystring = ele.Text;
        if (mystring == "your text")

  • @Premaja IOW: Loop over all elements with your text (locate them by whatever fits), check other attributes, pick the one element which fits the bill Mar 7, 2018 at 14:36
  • If the table is large, this brute force approach can be pretty expensive. Also in this case, is ele the element you want to click when the string matches? If so there is no need to find the element again, just call ele.Click(); in the if statement. Looks like a workable solution as far as I can see unless you don't want the element, but the row that contains it.
    – mrfreester
    Mar 7, 2018 at 16:58
  • <div class="k-grid-header">....</div>
    – Premaja
    Mar 12, 2018 at 6:55
  • <div class="k-grid-header">....</div> <div class="k-grid-content k-auto scrollable"> <table role="grid" datarole="selectable" class="k-selectable"> <colgroup>.....</colgroup> <tbody role="row group"> <tr data uid="6545644646" role="row" class="ng-scope"> <td class role="grid-cell">==$0 <span ng-bind="dataitem.Name" class="ng-binding">Template1</span></td> <td class role="grid cell"</td>
    – Premaja
    Mar 12, 2018 at 12:35
  • this is the code and I want the row to be selected using "Template1"
    – Premaja
    Mar 12, 2018 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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