如何在硒基中包含隐式等待?



如何在selenium base中包含隐含的_wait ?

from seleniumbase import SB

我正在访问谷歌帐户,我只管理与硒基础

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

最新更新