You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple example from [SeleniumBase/examples/cdp_mode/raw_gitlab.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_gitlab.py):
(If the CAPTCHA wasn't bypassed automatically, then `sb.uc_gui_click_captcha()` gets the job done.)
62
+
(If the CAPTCHA wasn't bypassed automatically when going to the URL, then `sb.uc_gui_click_captcha()` gets the job done with a mouse click from [PyAutoGUI](https://github.com/asweigart/pyautogui).)
64
63
65
-
Note that `PyAutoGUI` is an optional dependency. If calling a method that uses it when not already installed, then `SeleniumBase` installs `PyAutoGUI` at run-time.
64
+
ℹ️ Note that `PyAutoGUI` is an optional dependency. If calling a method that uses it when not already installed, then `SeleniumBase` installs `PyAutoGUI` at run-time.
66
65
67
66
--------
68
67
69
-
For some Cloudflare CAPTCHAs that appear within websites, you may need to use `sb.cdp.gui_click_element(selector)`instead (if the Turnstile wasn't bypassed automatically). Example: ([SeleniumBase/examples/cdp_mode/raw_planetmc.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_planetmc.py))
68
+
You can also use `sb.cdp.gui_click_element(selector)`to click on elements using `PyAutoGUI`. (This is useful when clicking inside `#shadow-root`.) Example:
In most cases, `sb.uc_gui_click_captcha()` is good enough for CF Turnstiles without needing `sb.cdp.gui_click_element(selector)`. (See [SeleniumBase/examples/cdp_mode/raw_planetmc.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_planetmc.py))
89
+
89
90
--------
90
91
91
92
### 🐙 Here are a few common `sb.cdp` methods:
92
93
93
94
*`sb.cdp.click(selector)` (Uses the CDP API to click)
94
-
*`sb.cdp.click_if_visible(selector)`
95
+
*`sb.cdp.click_if_visible(selector)` (Click if visible)
ℹ️ When available, calling `sb.METHOD()` redirects to `sb.cdp.METHOD()` because regular SB methods automatically call their CDP Mode counterparts to maintain stealth when CDP Mode is active.
<btranslate="no">Pure CDP Mode</b> doesn't use WebDriver for anything. The browser is launched using CDP, and all browser actions are performed using CDP (or <code>PyAutoGUI</code>). Initialization:
530
+
531
+
```python
532
+
from seleniumbase import sb_cdp
533
+
534
+
sb = sb_cdp.Chrome(url=None, **kwargs)
535
+
```
536
+
537
+
<btranslate="no">Pure CDP Mode</b> includes all methods from regular CDP Mode, except that they're called directly from <code>sb</code> instead of <code>sb.cdp</code>. Eg: <code>sb.gui_click_captcha()</code>. To quit a CDP-launched browser, use `sb.driver.stop()`.
538
+
539
+
Basic example from [SeleniumBase/examples/cdp_mode/raw_cdp_turnstile.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_cdp_turnstile.py):
540
+
541
+
```python
542
+
from seleniumbase import sb_cdp
543
+
544
+
url ="https://seleniumbase.io/apps/turnstile"
545
+
sb = sb_cdp.Chrome(url)
546
+
sb.gui_click_captcha()
547
+
sb.sleep(2)
548
+
sb.driver.stop()
549
+
```
550
+
551
+
Another example: ([SeleniumBase/examples/cdp_mode/raw_cdp_methods.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_cdp_methods.py))
552
+
553
+
```python
554
+
from seleniumbase import sb_cdp
555
+
556
+
url ="https://seleniumbase.io/demo_page"
557
+
sb = sb_cdp.Chrome(url)
558
+
sb.press_keys("input", "Text")
559
+
sb.highlight("button")
560
+
sb.type("textarea", "Here are some words")
561
+
sb.click("button")
562
+
sb.set_value("input#mySlider", "100")
563
+
sb.click_visible_elements("input.checkBoxClassB")
564
+
sb.select_option_by_text("#mySelect", "Set to 75%")
Copy file name to clipboardExpand all lines: help_docs/syntax_formats.md
+28-19Lines changed: 28 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,7 +119,10 @@ class MyTests(BaseTestCase):
119
119
The pytest framework comes with a unique system called fixtures, which replaces import statements at the top of Python files by importing libraries directly into test definitions. More than just being an import, a pytest fixture can also automatically call predefined <codetranslate="no">setUp()</code> and <codetranslate="no">tearDown()</code> methods at the beginning and end of test methods. To work, <codetranslate="no">sb</code> is added as an argument to each test method definition that needs SeleniumBase functionality. This means you no longer need import statements in your Python files to use SeleniumBase. <b>If using other pytest fixtures in your tests, you may need to use the SeleniumBase fixture (instead of <codetranslate="no">BaseCase</code> class inheritance) for compatibility reasons.</b> Here's an example of the <codetranslate="no">sb</code> fixture in a test that does not use Python classes:
The <codetranslate="no">sb</code> pytest fixture can also be used inside of a class. There is a slight change to the syntax because that means test methods must also include <codetranslate="no">self</code> in their argument definitions when test methods are defined. (The <codetranslate="no">self</code> argument represents the class object, and is used in every test method that lives inside of a class.) Once again, no import statements are needed in your Python files for this to work. Here's an example of using the <codetranslate="no">sb</code> fixture in a test method that lives inside of a Python class:
This is similar to the classic Page Object Model with <codetranslate="no">BaseCase</code> inheritance, except that this time we pass the <codetranslate="no">sb</code> pytest fixture from the test into the <codetranslate="no">sb</code> arg of the page object class method, (instead of passing <codetranslate="no">self</code>). Now that you're using <codetranslate="no">sb</code> as a pytest fixture, you no longer need to import <codetranslate="no">BaseCase</code> anywhere in your code. See the example below:
(See <ahref="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/boilerplates/samples/sb_swag_test.py">examples/boilerplates/samples/sb_swag_test.py</a> for the full test.)
@@ -197,8 +208,11 @@ class MyTests:
197
208
The pytest <codetranslate="no">request</code> fixture can be used to retrieve other pytest fixtures from within tests, such as the <codetranslate="no">sb</code> fixture. This allows you to have more control over when fixtures get initialized because the fixture no longer needs to be loaded at the very beginning of test methods. This is done by calling <codetranslate="no">request.getfixturevalue('sb')</code> from the test. Here's an example of using the pytest <codetranslate="no">request</code> fixture to load the <codetranslate="no">sb</code> fixture in a test method that does not use Python classes:
The pytest <codetranslate="no">request</code> fixture can also be used to get the <codetranslate="no">sb</code> fixture from inside a Python class. Here's an example of that:
0 commit comments