I have a table on my web page I am trying to automation that contains x number of rows (tr)s and under each (tr), there are a varying number of (td)s. In my flow, I have to check for the existence of a particular value (text) inside of a (td) for which I am doing the following:

  1. I obtain the total number of rows in the table and iterate through them.
  2. For/under each row, I am trying to find out the number of (td) that are present using

    List totalTDs = driver.findElements(By.xpath("...xpath of the td"));

  3. Then I do the following to decide whether to perform additional steps in this iteration:

    if (totalTDs.size() > 0) { //validate for the existence of text under the first (td) }

  4. Issue I am facing: Certain rows in my table do not have (td) under them and when selenium encounters one such row, it takes a lot of time to determine that the (td) is actually not present before the next line gets executed. When I try to print the number of (td) in one such row at this instant, I get a 0, which proves selenium in fact discovered the absence of (td) for that particular row.

My only concern is that the table I am dealing with has more than 100 rows and vast majority of them does not necessarily contain a (td). If selenium takes a lot of time to find out the (td) is actually not present, that drastically increases the execution time of my script. I am seeking help in understanding how I could get selenium to execute the code faster, if at all possible.

Any help in this regard would be greatly appreciated.


1 Answer 1


You have two alternatives:

1 - Decrease the timeout in this particular object:

driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
... do all your work
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECOND);

2 - Search directly for the links (a tag):

List links = driver.findElements(By.xpath(locator_for_td + " a"));

Both alternatives assume that the table is well loaded. A check for this condition may be necessary:

new WebDriverWait(driver, 10).until(
                                                  locator_for_td + " a"));
  • Sorry, I just realized my earlier question had some things that we included in a <> truncated. I have since updated my question by including brackets. I can try the first option today you suggested and let you know. Thanks, very much.
    – Em Revs
    Feb 21, 2019 at 22:44
  • I would agree with answer above and you directly check for text in td. So you can build an xpath like //table/tr/td[contains(.,'text you are looking for')] Feb 22, 2019 at 6:11
  • Joao Farias, you solution/suggestion worked for me. Thanks very much.
    – Em Revs
    Feb 22, 2019 at 22:18
  • You are welcome. Could you mark the answer as accepted? Feb 22, 2019 at 22:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.