Xpath sometimes work, sometime did not work

Hi All,

I am trying to click on DEF checkbox and for that i have written xpath as “//android.widget.TextView[@text=‘DEF’]”. But this xpath sometime work and sometime did not work. Can anyone let me know what could be the reason for this and what should be the proper xpath so that it works everytime?

Is there any transition time between the screens? Could need to use an implicit wait here:


I’d agreed with wreed. In general, when people do UI testing they forget to put the appropriate waits in. When you write a manual test you might have something like:

  • Open app
  • Click checkbox

But the reality is that we are doing:

  • Open app
  • Wait for checkbox to be clickable
  • Click checkbox

As humans, we don’t need explicit instructions. We inherently know to wait for something to be clickable before attempting to click it. Computer programs (e.g. automation) aren’t as smart. They need explicit instructions.

A quick way to see if you need an explicit wait is to just add a sleep statement. If you have:

  • find_element().click()

You want to change it to:

  • element = find_element()
  • sleep 2 seconds
  • element.click()

If this works then you can be fairly sure you need an explicit wait statement. Don’t just leave the sleep statement in. It will lead to slow and flakey tests.