0

I am trying to find the count of total row from table using below code but it's not working.

public IWebElement DocTable => driver.FindElement(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_grdCaseList_DXMainTable']/tbody/tr"));

int RowCount = DocTable.Count(); 

Below is the error I am getting:

Error CS1061 'IWebElement' does not contain a definition for 'Count' and no accessible extension method 'Count' accepting a first argument of type 'IWebElement' could be found (are you missing a using directive or an assembly reference?)

2 Answers 2

1

As per the documentation FindElement() finds the first IWebElement using the given method. Where as Count is a List property.

So to get a count of all the <tr> elements you need to create a List using FindElements() as follows:

public ReadOnlyCollection<IWebElement> DocTable => driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_grdCaseList_DXMainTable']/tbody//tr"));
int RowCount = DocTable.Count; 

Or

public IList<IWebElement> DocTable => driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_grdCaseList_DXMainTable']/tbody//tr"));
int RowCount = DocTable.Count;

As an alternative,

int RowCount = driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_grdCaseList_DXMainTable']/tbody//tr")).Count;

Note: Count is not a method but a property.

Sign up to request clarification or add additional context in comments.

5 Comments

i am still getting the error "Can not implicitly convert type 'OpenQA.Selenium.IWebElement' to 'System.Collections.Generic.List<OpenQA.Selenium.IWebElement>
@Gopal Updated the second option with list, should work now, How about the first option? let me know the status.
still no success by using both options. now getting error for code "DocTable.Count " 'A field initializer can not reference to the non static field'
@Gopal Can you remove the keyword public and retest and let me know the status?
@Gopal Well there were more changes with in the code. Can you please take the new lines of code and retest once again? With the current lines of code you shouldn't see the previous error.
0

FindElement returns a single IWebElement, the first row in the table in that case. Use FindElements to get a list of the <tr> elements

public List<IWebElement> DocTable => driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_grdCaseList_DXMainTable']/tbody/tr"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.