1

Right now, I have a method that will wait until an element is visible using:

WebDriverWait(self.driver, seconds).until(lambda s: s.find_element_by_xpath(path).is_displayed())

This works properly; however, it returns a boolean, rather than the element that it found. I would like it to return that element once it is found. I am doing this with the following:

def waituntil(path, seconds):
    WebDriverWait(self.driver, seconds).until(lambda s: s.find_element_by_xpath(path).is_displayed())
    ret = self.driver.find_element_by_xpath(path)
    return ret

Sure, this works. Unfortunately, it requires Selenium to find the same element twice, though, which adds waiting time (no matter how small). Is there a way I can return a web element using a waituntil (or similar functionality) by finding an element only once? So something that would allow the following:

ret = WebDriverWait(self.driver, seconds).until(lambda s: s.find_element_by_xpath(path).is_displayed())
ret.click()

I'm currently using:

Python 2.7
Windows 7
Selenium 2.4.4
Firefox 35.0.1

1 Answer 1

1

Use selenium.webdriver.support.expected_conditions.presence_of_element_located.

Based on the unofficial docs:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
ret = WebDriverWait(self.driver, seconds).until(
    expected_conditions.presence_of_element_located((By.XPATH, path)))
ret.click()

(Left out other imports and initialization for clarity, since you seem to have that stuff working. See the earlier link if you're running into issues.)

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

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.