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

How can I use SB without the "with" context #2277

Answered by mdmintz
iamumairayub asked this question in Q&A
Discussion options

We have an old codebase that we are converting to SeleniumBase and we need to use the SB class without the with context

from seleniumbase import SB
driver = SB(uc=True, 
 cap_string='{"selenoid:options": {"enableVNC": true}}',
)
driver.get('https://lumtest.com/myip.json')

But I am getting error

AttributeError: '_GeneratorContextManager' object has no attribute 'get'

Is it possible what I want?

You must be logged in to vote

The Driver() format can be used without a context manager:
Eg. https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_login_driver.py

from seleniumbase import Driver
driver = Driver()
# ...
driver.quit()

However, the standard SeleniumBase formats have clear setUp() and tearDown() steps defined in BaseCase, so there needs to be a way to guarantee that tearDown() will be reached when a test is run and completes. This is done via context manager or test method.

See https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md for all 23 Syntax Formats.

Replies: 2 comments 2 replies

Comment options

The Driver() format can be used without a context manager:
Eg. https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_login_driver.py

from seleniumbase import Driver
driver = Driver()
# ...
driver.quit()

However, the standard SeleniumBase formats have clear setUp() and tearDown() steps defined in BaseCase, so there needs to be a way to guarantee that tearDown() will be reached when a test is run and completes. This is done via context manager or test method.

See https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md for all 23 Syntax Formats.

You must be logged in to vote
0 replies
Answer selected by mdmintz
Comment options

Same need.
But Driver() does not support many SB features like xvfb
also it hasn't SB methods.

Challenge 1:
driver = Driver(xvfb=True)
TypeError: Driver() got an unexpected keyword argument 'xvfb'

Challenge 2:
driver .activate_cdp_mode(url)
AttributeError: 'Chrome' object has no attribute 'activate_cdp_mode'

You must be logged in to vote
2 replies
Comment options

SB(), (which has more methods than the Driver() format), must be used with a context manager. That's not going to change. For one, there's a lot more memory usage, and a context manager format means that memory can be cleared up cleanly after the with block is done.


CDP Mode is activated with the Driver() format like this: driver.uc_activate_cdp_mode() (assuming uc=True).

driver.uc_activate_cdp_mode(url)
Comment options

@mdmintz Thanks for explain

means that memory can be cleared up cleanly after the with block is done.

  • why not possible to quit sb like driver? why you say with block MUST close it?

  • what about xvfb?

  • Can you please say a mini example how can we create it inside init() of a class and have access it via self.sb?

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 によって変換されたページ (->オリジナル) /