Jin Jin - 2 months ago 14
C# Question

Selecting checkbox in a table using selenium

I'm trying to check a Checkbox in a table, it actually able to locate the Checkbox but it does not check/click the checkbox. The selectCheckbox.Click() is not working, can somebody explain to me why it's not checking/clicking the checkbox? thanks :)

my code is like this:

IWebElement table = WebBrowser.Current.FindElement(By.TagName("tbody"));

ReadOnlyCollection<IWebElement> allRows = table.FindElements(By.TagName("tr"));

for (int z = 0; z < allRows.Count; z++)
{
ReadOnlyCollection<IWebElement> cells = allRows[z].FindElements(By.TagName("td"));

for (int y = 0; y < cells.Count; y++)
{
var value = allRows[z].FindElements(By.TagName("td"))[y].Text;

if (value.Equals(tenant))
{
var selectCheckbox = allRows[z].FindElements(By.TagName("td"))[y - 1];
selectCheckbox.Click();
}
}
}

Answer

You are finding the td tag and trying to click on it. Try changing the code as shown below to find the input tag (Assuming that there is only one input tag in the required td)

from

var selectCheckbox = allRows[z].FindElements(By.TagName("td"))[y - 1];

to

var selectCheckbox = allRows[z].FindElements(By.TagName("input"))[y - 1].FindElement(By.TagName("input"));

Hope this helps you.

Comments