1

I have a file called webdriver.py which implements methods from selenium.webdriver library. Had a wait-function that were handling most of the cases I need:

def wait_for(self, func, target=None, timeout=None, **kwargs):
    timeout = timeout or self.timeout
    try:
        return WebDriverWait(self, timeout).until(func)
    except TimeoutException:
        if not target:
            raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip())
        raise NoSuchElementException(target)

Where func is a selector.

The problem is that sometimes the DOM element would be invisible, causing exception and test failure. So I would like to extend wait_for to also wait for the element to become visible.

Something like

def wait_for(self, func, target=None, timeout=None, **kwargs):
    timeout = timeout or self.timeout
    try:
        return WebDriverWait(self, timeout).until(EC.element_to_be_clickable(func)).until(func)
    except TimeoutException:
        if not target:
            raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip())
        raise NoSuchElementException(target)

EC is a selenium.driver.expected_conditions

This would not work of course - either until().until() syntax is not supported.. or something else happens, like EC doesn't exist.

Any ideas?

1 Answer 1

1

You can use EC.presence_of_element_located for waiting an element in DOM to visible, no need for second .until.

def wait_for(self, func, target=None, timeout=self.timeout):
    try:
        WebDriverWait(self, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, func)))
    except TimeoutException:
        if not target:
            raise WebDriverException('Wait for: "%s" failed!' % inspect.getsource(func).strip())
        raise NoSuchElementException(target)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for help, I also used "from selenium.webdriver.support import expected_conditions as EC" to fix EC problem.
I also think it should be: "By.ByCssSelector" according to seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…
@h3d0 it should be By.CSS_SELECTOR for python check selenium-python.readthedocs.org/…
Indeed,By.CSS_SELECTOR it is.

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.