Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.open("https://browserscan.net/bot-detection")
sb.goto("https://browserscan.net/bot-detection")
sb.sleep(3)
sb.quit()
```
Expand Down Expand Up @@ -105,7 +105,7 @@ with sync_playwright() as p:
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(locale="en", ad_block=True)
sb.open("https://browserscan.net/bot-detection")
sb.goto("https://browserscan.net/bot-detection")
sb.flash("Test Results", duration=1.5, pause=0.5)
sb.assert_element('strong:contains("Normal")')
print("Bot Not Detected")
Expand Down Expand Up @@ -242,7 +242,7 @@ BaseCase.main(__name__, __file__) # Call pytest

class MyTestClass(BaseCase):
def test_swag_labs(self):
self.open("https://www.saucedemo.com")
self.goto("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
Expand Down Expand Up @@ -362,7 +362,7 @@ BaseCase.main(__name__, __file__)

class TestSimpleLogin(BaseCase):
def test_simple_login(self):
self.open("seleniumbase.io/simple/login")
self.goto("seleniumbase.io/simple/login")
self.type("#username", "demo_user")
self.type("#password", "secret_pass")
self.click('a:contains("Sign in")')
Expand All @@ -379,7 +379,7 @@ class TestSimpleLogin(BaseCase):
from seleniumbase import SB

with SB() as sb:
sb.open("seleniumbase.io/simple/login")
sb.goto("seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
Expand All @@ -397,7 +397,7 @@ from seleniumbase import Driver

driver = Driver()
try:
driver.open("seleniumbase.io/simple/login")
driver.goto("seleniumbase.io/simple/login")
driver.type("#username", "demo_user")
driver.type("#password", "secret_pass")
driver.click('a:contains("Sign in")')
Expand Down Expand Up @@ -549,7 +549,7 @@ BaseCase.main(__name__, __file__)

class MyTestClass(BaseCase):
def test_swag_labs(self):
self.open("https://www.saucedemo.com")
self.goto("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
Expand Down Expand Up @@ -582,7 +582,8 @@ class MyTestClass(BaseCase):
<h3><img src="https://seleniumbase.github.io/img/logo7.png" title="SeleniumBase" width="32" /> Here are some common SeleniumBase methods:</h3>

```python
self.open(url) # Navigate the browser window to the URL.
self.goto(url) # Navigate the browser window to the URL.
self.open(url) # Same as `self.goto(url)`
self.activate_cdp_mode() # Activate CDP Mode from UC Mode.
self.type(selector, text) # Update the field with the text.
self.click(selector) # Click the element with the selector.
Expand Down Expand Up @@ -958,7 +959,7 @@ BaseCase.main(__name__, __file__)
class MyTestClass(BaseCase):

def test_find_army_of_robots_on_xkcd_desert_island(self):
self.open("https://xkcd.com/731/")
self.goto("https://xkcd.com/731/")
self.assert_element("div#ARMY_OF_ROBOTS", timeout=1) # This should fail
```

Expand Down Expand Up @@ -1162,7 +1163,7 @@ pytest [YOUR_TEST_FILE.py] --with-db-reporting --with-s3-logging
🔵 **Navigating to a web page: (and related commands)**

```python
self.open("https://xkcd.com/378/") # This method opens the specified page.
self.goto("https://xkcd.com/378/") # This method opens the specified page.

self.go_back() # This method navigates the browser to the previous page.

Expand Down Expand Up @@ -1299,7 +1300,7 @@ def is_there_a_cloaked_klingon_ship_on_this_page():

```python
if self.is_text_visible("You Shall Not Pass!", "h1"):
self.open("https://www.youtube.com/watch?v=3xYXUeSmb-Y")
self.goto("https://www.youtube.com/watch?v=3xYXUeSmb-Y")
```

<div></div>
Expand Down Expand Up @@ -1444,7 +1445,7 @@ self.execute_script("return jQuery('textarea')[2].value") # Returns the css "va
```python
start_page = "https://xkcd.com/465/"
destination_page = "https://github.com/seleniumbase/SeleniumBase"
self.open(start_page)
self.goto(start_page)
referral_link = '''<a class='analytics test' href='%s'>Free-Referral Button!</a>''' % destination_page
self.execute_script('''document.body.innerHTML = \"%s\"''' % referral_link)
self.click("a.analytics") # Clicks the generated button
Expand All @@ -1464,7 +1465,7 @@ BaseCase.main(__name__, __file__)

class DeferredAssertTests(BaseCase):
def test_deferred_asserts(self):
self.open("https://xkcd.com/993/")
self.goto("https://xkcd.com/993/")
self.wait_for_element("#comic")
self.deferred_assert_element('img[alt="Brand Identity"]')
self.deferred_assert_element('img[alt="Rocket Ship"]') # Will Fail
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class MyTestClass(BaseCase):
def test_basics(self):
self.open("https://www.saucedemo.com")
self.goto("https://www.saucedemo.com")
self.type("#user-name", "standard_user")
self.type("#password", "secret_sauce\n")
self.assert_element("div.inventory_list")
Expand Down
2 changes: 1 addition & 1 deletion examples/behave_bdd/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ from behave import step
@step("Open the Swag Labs Login Page")
def go_to_swag_labs(context):
sb = context.sb
sb.open("https://www.saucedemo.com")
sb.goto("https://www.saucedemo.com")
sb.clear_local_storage()


Expand Down
2 changes: 1 addition & 1 deletion examples/behave_bdd/features/steps/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@step("Open the Calculator App")
def go_to_calculator(context):
context.sb.open("https://seleniumbase.io/apps/calculator")
context.sb.goto("https://seleniumbase.io/apps/calculator")


@step("Press C")
Expand Down
2 changes: 1 addition & 1 deletion examples/behave_bdd/features/steps/fail_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@step("Open the Fail Page")
def go_to_error_page(context):
context.sb.open("https://seleniumbase.io/error_page/")
context.sb.goto("https://seleniumbase.io/error_page/")


@step("Fail test on purpose")
Expand Down
2 changes: 1 addition & 1 deletion examples/behave_bdd/features/steps/swag_labs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@step("Open the Swag Labs Login Page")
def go_to_swag_labs(context):
sb = context.sb
sb.open("https://www.saucedemo.com")
sb.goto("https://www.saucedemo.com")
sb.clear_local_storage()


Expand Down
2 changes: 1 addition & 1 deletion examples/boilerplates/classic_obj_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class DataPage:
def go_to_data_url(self, sb):
sb.open("data:text/html,<p>Hello!</p><input />")
sb.goto("data:text/html,<p>Hello!</p><input />")

def add_input_text(self, sb, text):
sb.type("input", text)
Expand Down
7 changes: 4 additions & 3 deletions examples/boilerplates/samples/google_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
class GoogleTests(BaseCase):
def test_google_dot_com(self):
if self.headless:
self.open_if_not_url("about:blank")
self.goto_if_not_url("about:blank")
print("\n Skipping test in headless mode.")
self.skip("Skipping test in headless mode.")
if not self.undetectable:
self.get_new_driver(undetectable=True)
self.driver.get("https://google.com/ncr")
self.goto("https://google.com/ncr")
self.click_if_visible('button:contains("Accept all")')
self.assert_title_contains("Google")
self.sleep(0.05)
self.save_screenshot_to_logs() # ("./latest_logs" folder)
self.type(HomePage.search_box, "github.com")
self.type(HomePage.search_box, "GitHub")
self.assert_element(HomePage.search_button)
self.assert_element(HomePage.feeling_lucky_button)
self.click(HomePage.search_button)
self.sleep(1)
self.assert_text("github.com", ResultsPage.search_results)
2 changes: 1 addition & 1 deletion examples/boilerplates/samples/sb_swag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class LoginPage:
def login_to_swag_labs(self, sb: BaseCase, username):
sb.open("https://www.saucedemo.com")
sb.goto("https://www.saucedemo.com")
sb.type("#user-name", username)
sb.type("#password", "secret_sauce")
sb.click('input[type="submit"]')
Expand Down
2 changes: 1 addition & 1 deletion examples/boilerplates/samples/swag_labs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class LoginPage:
def login_to_swag_labs(self, sb: BaseCase, username):
sb.open("https://www.saucedemo.com")
sb.goto("https://www.saucedemo.com")
sb.type("#user-name", username)
sb.type("#password", "secret_sauce")
sb.click('input[type="submit"]')
Expand Down
10 changes: 5 additions & 5 deletions examples/boilerplates/samples/test_page_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class GooglePage:
def go_to_google(self, sb):
sb.driver.get("https://google.com/ncr")
sb.goto("https://google.com/ncr")
sb.click_if_visible('button:contains("Accept all")')

def assert_google_title(self, sb):
Expand All @@ -19,8 +19,8 @@ def hide_sign_in_pop_up(self, sb):

def do_search(self, sb, search_term):
sb.sleep(0.05)
sb.click('[title="Search"]')
sb.type('[title="Search"]', search_term + "\n")
sb.type('[name="q"]', search_term)
sb.click('[value="Google Search"]')

def click_search_result(self, sb, content):
sb.click('a:contains("%s")' % content)
Expand All @@ -36,12 +36,12 @@ def do_search_and_click(self, sb, search_term):
class MyTests(BaseCase):
def test_page_objects(self):
if self.headless:
self.open_if_not_url("about:blank")
self.goto_if_not_url("about:blank")
print("\n Skipping test in headless mode.")
self.skip("Skipping test in headless mode.")
if not self.undetectable:
self.get_new_driver(undetectable=True)
search_term = "SeleniumBase.io Docs"
search_term = "site:seleniumbase.io Docs"
expected_text = "SeleniumBase"
GooglePage().go_to_google(self)
GooglePage().assert_google_title(self)
Expand Down
2 changes: 1 addition & 1 deletion examples/boilerplates/sb_fixture_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class DataPage:
def go_to_data_url(self, sb):
sb.open("data:text/html,<p>Hello!</p><input />")
sb.goto("data:text/html,<p>Hello!</p><input />")

def add_input_text(self, sb, text):
sb.type("input", text)
Expand Down
16 changes: 9 additions & 7 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

That disconnects WebDriver from Chrome (which prevents detection), and gives you access to `sb.cdp` methods (which don't trigger anti-bot checks).

> (**New:** Calling **`sb.open(url)`** from UC Mode also activates CDP Mode now.)
> (**New:** Calling **`sb.goto(url)`** from UC Mode also activates CDP Mode now.)

Simple example from [SeleniumBase/examples/cdp_mode/raw_gitlab.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_gitlab.py):

Expand Down Expand Up @@ -163,7 +163,7 @@ with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
sb.sleep(2)
sb.highlight_overlay("div.pokemon-ability-info")
sb.sleep(2)
sb.open("https://events.pokemon.com/EventLocator/")
sb.goto("https://events.pokemon.com/EventLocator/")
sb.sleep(2)
sb.click('span:contains("Championship")')
sb.sleep(2)
Expand Down Expand Up @@ -372,6 +372,7 @@ with SB(uc=True, test=True, locale="en", pls="none") as sb:

```python
sb.cdp.get(url, **kwargs)
sb.cdp.goto(url, **kwargs) # Same as sb.cdp.get(url, **kwargs)
sb.cdp.open(url, **kwargs) # Same as sb.cdp.get(url, **kwargs)
sb.cdp.reload(ignore_cache=True, script_to_evaluate_on_load=None)
sb.cdp.refresh(*args, **kwargs)
Expand Down Expand Up @@ -582,7 +583,7 @@ from seleniumbase import sb_cdp
sb = sb_cdp.Chrome(url)
```

<b translate="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()`.
<b translate="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.quit()`.

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

Expand All @@ -595,7 +596,7 @@ sb.solve_captcha()
sb.assert_element("img#captcha-success")
sb.set_messenger_theme(location="top_left")
sb.post_message("SeleniumBase wasn't detected", duration=3)
sb.driver.stop()
sb.quit()
```

Another example: ([SeleniumBase/examples/cdp_mode/raw_cdp_methods.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_cdp_methods.py))
Expand All @@ -617,10 +618,10 @@ sb.gui_click_element("#checkBox1")
sb.gui_drag_and_drop("img#logo", "div#drop2")
sb.nested_click("iframe#myFrame3", ".fBox")
sb.sleep(2)
sb.driver.stop()
sb.quit()
```

ℹ️ Even if you don't call `sb.driver.stop()`, the browser still quits after the script goes out-of-scope.
ℹ️ Even if you don't call `sb.quit()`, the browser still quits after the script goes out-of-scope.

----

Expand All @@ -639,7 +640,8 @@ Methods: (Sometimes `tab` is named `page` in examples)

```python
await tab.get(url="about:blank")
await tab.open(url="about:blank")
await tab.goto(url="about:blank") # Same as await tab.get(url)
await tab.open(url="about:blank") # Same as await tab.get(url)
await tab.find(text, best_match=False, timeout=10) # text can be selector
await tab.find_all(text, timeout=10) # text can be selector
await tab.select(selector, timeout=10)
Expand Down
12 changes: 9 additions & 3 deletions examples/cdp_mode/playwright/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,17 @@ To use an authenticated proxy in Stealthy Playwright Mode, **do these two things

⚠️ If any trouble with the above, set `use_chromium=True` so that you can use the base Chromium browser, which still allows extensions, unlike regular branded Chrome, which removed the `--load-extension` command-line switch. (*An extension is used to set the auth for the proxy, which is needed when CDP can't set the proxy alone, such as for navigation after the initial page load*).

In the sync format, use `sb.open(url)` to open the url before connecting Playwright:
In the sync format, use `sb.goto(url)` to open the url before connecting Playwright:

```python
sb = sb_cdp.Chrome(use_chromium=True, proxy="user:pass@server:port")
sb.open(url)
sb.goto(url)
endpoint_url = sb.get_endpoint_url()
# ...
```

In the async format, use, `driver.get(url)` to open the url before connecting Playwright:

```python
driver = await cdp_driver.start_async(use_chromium=True, proxy="user:pass@server:port")
await driver.get(url)
Expand All @@ -232,22 +234,25 @@ endpoint_url = driver.get_endpoint_url()
```

Here's an example of using an authenticated proxy with Stealthy Playwright Mode:<br />(The URL is opened before attaching Playwright so that proxy settings take effect)

```python
from playwright.sync_api import sync_playwright
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(use_chromium=True, proxy="user:pass@server:port")
sb.open(url)
sb.goto(url)
endpoint_url = sb.get_endpoint_url()

with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(endpoint_url)
page = browser.contexts[0].pages[0]
# ...
```

(Fill in the `url` and the `proxy` details to complete the script.)

Here's the same thing for the `async` format:

```python
import asyncio
from playwright.async_api import async_playwright
Expand All @@ -267,6 +272,7 @@ if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
```

(Fill in the `url` and the `proxy` details to complete the script.)

--------
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/playwright/raw_indeed_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.open("https://www.indeed.com/companies/search")
sb.goto("https://www.indeed.com/companies/search")
sb.sleep(2)
sb.solve_captcha()
sb.sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/playwright/raw_walmart_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(locale="en", guest=True)
sb.open("https://www.walmart.com/")
sb.goto("https://www.walmart.com/")
endpoint_url = sb.get_endpoint_url()

with sync_playwright() as p:
Expand Down
Loading