Python Forum

Full Version: Need Help with Selenium Explicit Wait
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to wait for a specific 'div' id to be present and am struggling to understand explicit wait syntax. Here is the tag I want to wait for:

Output:
<div id="StepTwoAcceptedPanel" data-i18n="[html]report.AcceptMainContentLocalizeResource1"><h2 class="sp_h3_t_form">

I can't figure out which of the predefined conditions I should be using. I want to wait for the div tag with that id. Can someone advise? TIA.
it would be most helpful if you would show your code.
I don't have any code because I don't know what to do. Although likely wrong this is my guess:

.
.
.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
.
.
.
try:
    wait=WebDriverWait(driver,5)
    wait.until(ec.visibility_of_element_located(By.id,'StepTwoAcceptedPanel'))
except (TimeOutException):
    print('Not found')
    exit(-1)
print('Success')
Presumably it will either timeout or find that id.
I struggle with these myself.
here's one I created recently where I am waiting for the page number to change
It's quite different from yours, but perhaps useful, perhaps not:
        try:
            wait_value = f"Page {pageno} of"
            WebDriverWait(self.browser, 30).until(
                lambda zig: "Page {:}".format(pageno) in zig.find_element(By.CLASS_NAME, "pageinfo").text)
            # WebDriverWait(driver, timeout).until(lambda driver: "Page {:}".format(current_page) in driver.find_element(By.ID, "SEARCHBASE").text)
        except TimeoutException:
            print(f"Found by time out page: {pageno}")
        except StaleElementReferenceException:
            print(f"StaleElement page {pageno} auto continue")
            wait_value = f"Page {pageno} of"
            WebDriverWait(self.browser, 30).until(
                lambda zig: "Page {:}".format(pageno) in zig.find_element(By.CLASS_NAME, "pageinfo").text)
I've made some progress (I think). I came up with this code:

try:
    wait=WebDriverWait(browser,5)
    wait.until(EC.presence_of_element_located(By.ID('StepTwoAcceptedPanel')))
except (TimeoutException):
    print('Submission failed')
    browser.quit()
    exit(-1)
print('Success')
browser.quit()
Unfortunately I still am having a syntax problem. Although I have seen other similar solutions, the syntax seems to me to be correct. However, I get this traceback:

Output:
Traceback (most recent call last): File "./donotcall.py", line 183, in <module> wait.until(EC.presence_of_element_located(By.ID('StepTwoAcceptedPanel'))) TypeError: 'str' object is not callable
Boy this was confusing. I finally got it. Simple parens error:

wait.until(EC.presence_of_element_located((By.ID,'StepTwoAcceptedPanel')))
5 seconds may not be enough. If you put 30 seconds, it will only take that long if the conditions are never met. If conditions are met after only .5 seconds, the wait period ends.