Tanya
Tanya

Reputation: 309

Web Table handling Selenium Webdriver c#

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?

The table structure looks like below

Upvotes: 1

Views: 342

Answers (2)

jkindwall
jkindwall

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

mrfreester
mrfreester

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

Related Questions