Python Forum
Need Help with Selenium Explicit Wait
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help with Selenium Explicit Wait
#1
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.
Reply
#2
it would be most helpful if you would show your code.
Reply
#3
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.
Reply
#4
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)
Reply
#5
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
Reply
#6
Boy this was confusing. I finally got it. Simple parens error:

wait.until(EC.presence_of_element_located((By.ID,'StepTwoAcceptedPanel')))
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  selenium wait for text question Larz60+ 3 2,501 Oct-25-2021, 09:50 AM
Last Post: Larz60+
  drive wait in try windows11 0 1,802 Apr-07-2020, 01:50 PM
Last Post: windows11
  wait for element and click windows11 2 2,603 Mar-21-2020, 09:23 PM
Last Post: windows11
  Python Import Sybase Module returning non explicit error thebeefes 2 5,852 Mar-14-2019, 07:32 PM
Last Post: Carlos
  wait.until(EC.element_to_be_clickable) failed to click gahhon 4 7,864 Feb-23-2019, 04:58 AM
Last Post: gahhon
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 3,588 Nov-03-2017, 08:41 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020