Search code examples
pythonseleniumbase

How to include implicit wait in seleniumbase?


how could i include implicitly_wait in selenium base?

from seleniumbase import SB

I'm accessing the google account and I only managed with selenium base


Solution

  • The SeleniumBase waits are already included with default timeout values. Without the waits, a script may look like this:

    from seleniumbase import SB
    
    with SB() as sb:  # By default, browser="chrome" if not set.
        sb.open("https://seleniumbase.io/realworld/login")
        sb.type("#username", "demo_user")
        sb.type("#password", "secret_pass")
        sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")  # 6-digit
        sb.assert_text("Welcome!", "h1")
        sb.highlight("img#image1")  # A fancier assert_element() call
        sb.click('a:contains("This Page")')  # Use :contains() on any tag
        sb.click_link("Sign out")  # Link must be "a" tag. Not "button".
        sb.assert_element('a:contains("Sign in")')
        sb.assert_exact_text("You have been signed out!", "#top_message")
    

    But with the waits, a script might look like this: (With timeout=TIMEOUT added)

    from seleniumbase import SB
    
    with SB() as sb:
        sb.open("https://seleniumbase.io/realworld/login")
        sb.type("#username", "demo_user", timeout=7)
        sb.type("#password", "secret_pass", timeout=8)
        sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")
        sb.assert_text("Welcome!", "h1", timeout=9)
        sb.highlight("img#image1")
        sb.click('a:contains("This Page")', timeout=6)
        sb.click_link("Sign out", timeout=5)
        sb.assert_element('a:contains("Sign in")', timeout=4)
        sb.assert_exact_text("You have been signed out!", "#top_message", timeout=3)
    

    Most methods have a timeout arg that can be included to change the default waiting time, as seen above.

    Here's anther example that uses the SB Context Manager:

    with SB(test=True, rtf=True, demo=True) as sb:
        sb.open("seleniumbase.github.io/demo_page")
        sb.type("#myTextInput", "This is Automated")
        sb.assert_text("This is Automated", "#myTextInput")
        sb.assert_text("This Text is Green", "#pText")
        sb.click('button:contains("Click Me")')
        sb.assert_text("This Text is Purple", "#pText")
        sb.click("#checkBox1")
        sb.assert_element_not_visible("div#drop2 img#logo")
        sb.drag_and_drop("img#logo", "div#drop2")
        sb.assert_element("div#drop2 img#logo")