Reputation: 309
I have several rows and two columns. The column names are Town and country. I need to find the country "UK" in the column and click on the town in the adjacent row. Please tell me how to do it?
Upvotes: 1
Views: 342
Reputation: 3896
Try using the TableDriver extension to .NET WebDriver (https://github.com/jkindwall/TableDriver.NET). While the long, xpath expression shown in the accepted answer will work, its not very readable. With TableDriver, you can do it like this:
Table table = Table.Create(driver.FindElement(By.CssSelector("table.ColorTable")));
TableCell cell = table.FindCell("Country=UK", "Town");
cell.FindElement(By.TagName("a")).Click();
Upvotes: 0
Reputation: 1991
xpath
allows you to traverse back up the tree, in this case using the ancestor
axes. Try this:
IWebElement town = By.Xpath("//*[@headers='ctry'][text()='UK']/ancestor::tr//*[@headers='twn']/a");
Then you can just click it:
town.Click();
Upvotes: 1