如何在seleniumbase中包含隐式等待?

问题描述 投票:0回答:1

我怎样才能在硒基中包含implicitly_wait?

from seleniumbase import SB

我正在访问谷歌帐户,并且我只使用 selenium 基础进行管理

python selenium-webdriver seleniumbase
1个回答
2
投票

SeleniumBase 等待已包含在默认超时值中。如果没有等待,脚本可能如下所示:

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")

但是随着等待,脚本可能看起来像这样:(添加了

timeout=TIMEOUT

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)

大多数方法都有一个

timeout
参数,可以包含它来更改默认等待时间,如上所示。

这是使用 SB 上下文管理器的另一个示例:

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")
© www.soinside.com 2019 - 2024. All rights reserved.