Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sb.__exit__() #3418

Answered by mdmintz
Kamran-ov asked this question in Q&A
Jan 14, 2025 · 1 comments · 1 reply
Discussion options

I am using SB within a class and it is being generated as a global object within a class and then used by different methods.

Now in certain conditions (when my browser got detected as a bot), I want to force the close of the current open one, before recreating a new one. However, I cannot figure out a way to do that.
I always get the below error:
'BaseCase' object has no attribute ...

I wrote a simple script instead of getting all my class here, but the logic should be the same

from seleniumbase import SB
from time import sleep
sb = SB(uc=True,
 headless2=False,
 block_images=True,
 )
sb = sb.__enter__()
sleep(2)
print("I did all my script and finished with it")
# Exit the SeleniumBase instance
# sb.__exit__()
# sb.__exit__(None, None, None)
sb.quit()

Shouldnt it have exit() method ?

You must be logged in to vote

That's not one of the officially supported SeleniumBases Syntax Formats.

SB() must be used within a Python context manager (using the with statement).

Eg.

from seleniumbase import SB
with SB() as sb:
 sb.open("seleniumbase.io/simple/login")
 sb.type("#username", "demo_user")
 sb.type("#password", "secret_pass")
 sb.click('a:contains("Sign in")')
 sb.assert_exact_text("Welcome!", "h1")
 sb.assert_element("img#image1")
 sb.highlight("#image1")
 sb.click_link("Sign out")
 sb.assert_text("signed out", "#top_message")

Replies: 1 comment 1 reply

Comment options

That's not one of the officially supported SeleniumBases Syntax Formats.

SB() must be used within a Python context manager (using the with statement).

Eg.

from seleniumbase import SB
with SB() as sb:
 sb.open("seleniumbase.io/simple/login")
 sb.type("#username", "demo_user")
 sb.type("#password", "secret_pass")
 sb.click('a:contains("Sign in")')
 sb.assert_exact_text("Welcome!", "h1")
 sb.assert_element("img#image1")
 sb.highlight("#image1")
 sb.click_link("Sign out")
 sb.assert_text("signed out", "#top_message")
You must be logged in to vote
1 reply
Comment options

Also see #3482 (comment) for getting around that restriction.

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /