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?
1 Answer
string mystring;
IList<IWebElement> mylist;
Thread.Sleep(2000);
//Search Table
mylist = driver.FindElements(By.XPath(Table));
if (mylist.Count >= 1)
{
foreach (IWebElement ele in mylist)
{
mystring = ele.Text;
if (mystring == "your text")
{
Thread.Sleep(3000);
driver.FindElement(By.LinkText(mystring)).Click();
break;
}
}
}
-
@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 callele.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. Mar 7, 2018 at 16:58 -
-
<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>– PremajaMar 12, 2018 at 12:35
-
this is the code and I want the row to be selected using "Template1"– PremajaMar 12, 2018 at 12:36
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.