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?
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 – Peter M. - stands for Monica Mar 7 '18 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. – mrfreester Mar 7 '18 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> – Premaja Mar 12 '18 at 12:35
-
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 '18 at 17:48