3

With reference to this question https://sqa.stackexchange.com/questions/3481/quicker-way-to-assert-that-an-element-does-not-exist I'm trying to write an element_is_not_present helper function:

def element_is_not_present(selector):
    self.d.implicitly_wait(0)
    number_present = len(self.d.find_elements_by_css_selector(selector))
    self.d.implicitly_wait(10)

    return number_present == 0

find_elements_by_css_selector will wait for the implicit wait to timeout before returning so I have added mutators before and after the call to speed up the method. The issue comes in restoring the implicit wait, apparently there is no getter for the implicit wait value, so I must hard code the restore value causing issues when using a different base values.

The question is, how can I speed up my method whilst negating this issue? Failing this, maybe there is another way of writing the helper method or dealing with the explicit wait?


My current solution is to use my own implicit wait default value global but this is far from ideal.

Community
  • 1
  • 1
thodic
  • 2,229
  • 1
  • 19
  • 35
  • Forget about implicit waits and use explicit waits only: http://stackoverflow.com/a/28067495/771848. – alecxe May 18 '15 at 10:34
  • @alecxe My issue is `find_elements_by_css_selector` is tied to implicit waits. If there is a solid way of implementing `element_is_not_present` using explicit waits I would love to see it. – thodic May 18 '15 at 10:43
  • There is no need to use waits for checking the presence of element in your case. I'd avoid changing the implicit wait in the first place (leave it 0 as by default) and, hence, avoiding the delay before selenium tries to find an element. Hope that makes sense. – alecxe May 18 '15 at 10:46
  • Plus, I would use EAFP approach here - try/except finding the element and catch "NoSuchElementException". – alecxe May 18 '15 at 10:47
  • @alacxe This is a solid approach. However, I have other parts of my test suite which rely on implicit waits meaning I must maintain the value > 0, and without an implicit wait of 0 the EAFP approach becomes very slow. – thodic May 18 '15 at 10:53

1 Answers1

0

the short answer is: do not use implicit wait. use explicit wait in all your code.

the bit longer answer: this is exactly one of the problems why people, me included, advise you to not use implict wait. using implict wait there is no way to test for absence of element without waiting for the timeout. and the only way to avoid that is by disabling the implicit wait during the absence test. or by using explicit wait.

here is even more longer answer: When to use explicit wait vs implicit wait in Selenium Webdriver?

also interesting: How to test for absence of an element without waiting for a 30 second timeout

Lesmana
  • 25,663
  • 9
  • 82
  • 87