Skip to content

Commit 82c81a3

Browse files
committed
Add / update CDP Mode examples
1 parent dc2eb0e commit 82c81a3

File tree

10 files changed

+117
-9
lines changed

10 files changed

+117
-9
lines changed

examples/cdp_mode/raw_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
async def main():
9-
url = "seleniumbase.io/simple/login"
9+
url = "https://seleniumbase.io/simple/login"
1010
driver = await cdp_driver.start_async()
1111
page = await driver.get(url, lang="en")
1212
print(await page.get_title())

examples/cdp_mode/raw_basic_async.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
async def main():
7-
url = "seleniumbase.io/simple/login"
7+
url = "https://seleniumbase.io/simple/login"
88
driver = await cdp_driver.start_async()
99
page = await driver.get(url, lang="en")
1010
print(await page.get_title())
@@ -21,7 +21,6 @@ async def main():
2121
driver.stop()
2222

2323
if __name__ == "__main__":
24-
# Call an async function with awaited methods
2524
loop = asyncio.new_event_loop()
2625
with decorators.print_runtime("raw_basic_async.py"):
2726
loop.run_until_complete(main())

examples/cdp_mode/raw_basic_cdp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://seleniumbase.io/simple/login"
4+
sb = sb_cdp.Chrome(url)
5+
sb.type("#username", "demo_user")
6+
sb.type("#password", "secret_pass")
7+
sb.click('a:contains("Sign in")')
8+
sb.assert_exact_text("Welcome!", "h1")
9+
sb.assert_element("img#image1")
10+
sb.highlight("#image1")
11+
top_nav = sb.find_element("div.topnav")
12+
links = top_nav.query_selector_all("a")
13+
for nav_item in links:
14+
print(nav_item.text)
15+
sb.click_link("Sign out")
16+
sb.assert_text("signed out", "#top_message")
17+
sb.driver.stop()

examples/cdp_mode/raw_cdp_login.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from seleniumbase import decorators
2+
from seleniumbase import sb_cdp
3+
4+
5+
def main():
6+
url = "https://seleniumbase.io/simple/login"
7+
sb = sb_cdp.Chrome(url)
8+
sb.type("#username", "demo_user")
9+
sb.type("#password", "secret_pass")
10+
sb.click('a:contains("Sign in")')
11+
sb.assert_exact_text("Welcome!", "h1")
12+
sb.assert_element("img#image1")
13+
sb.highlight("#image1")
14+
top_nav = sb.find_element("div.topnav")
15+
links = top_nav.query_selector_all("a")
16+
for nav_item in links:
17+
print(nav_item.text)
18+
sb.click_link("Sign out")
19+
sb.assert_text("signed out", "#top_message")
20+
sb.driver.stop()
21+
22+
23+
if __name__ == "__main__":
24+
with decorators.print_runtime("raw_cdp_login.py"):
25+
main()

examples/cdp_mode/raw_cf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
with SB(uc=True, test=True, locale="en", guest=True) as sb:
55
url = "https://www.cloudflare.com/login"
66
sb.activate_cdp_mode(url)
7-
sb.sleep(4)
7+
sb.sleep(3)
88
sb.uc_gui_handle_captcha() # PyAutoGUI press Tab and Spacebar
9-
sb.sleep(2)
9+
sb.sleep(3)
1010

1111
with SB(uc=True, test=True, locale="en", guest=True) as sb:
1212
url = "https://www.cloudflare.com/login"
1313
sb.activate_cdp_mode(url)
1414
sb.sleep(4)
1515
sb.uc_gui_click_captcha() # PyAutoGUI click. (Linux needs it)
16-
sb.sleep(2)
16+
sb.sleep(3)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, guest=True) as sb:
4+
url = "https://www.cloudflare.com/login"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(3)
7+
sb.solve_captcha()
8+
sb.sleep(3)

examples/cdp_mode/raw_homedepot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, ad_block=True) as sb:
4+
url = "https://www.homedepot.com/"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(1.8)
7+
search_box = "input#typeahead-search-field-input"
8+
search = "Computer Chair"
9+
category = "Gaming Chairs"
10+
required_text = "Chair"
11+
sb.click(search_box)
12+
sb.sleep(1.2)
13+
sb.press_keys(search_box, search)
14+
sb.sleep(0.6)
15+
sb.click("button#typeahead-search-icon-button")
16+
sb.sleep(3.8)
17+
sb.click('a[aria-label="%s"]' % category)
18+
sb.sleep(3.2)
19+
print('*** Home Depot Search for "%s":' % search)
20+
print(' (Results must contain "%s".)' % required_text)
21+
unique_item_text = []
22+
items = sb.find_elements('div[data-testid="product-pod"]')
23+
for item in items:
24+
if required_text in item.text:
25+
description = item.querySelector(
26+
'span[data-testid="attribute-product-label"]'
27+
)
28+
if description and description.text not in unique_item_text:
29+
unique_item_text.append(description.text)
30+
print("* " + description.text)
31+
price = item.querySelector('[class*="sm:sui-text-4xl"]')
32+
if price:
33+
price_text = "$%s" % price.text
34+
print(" (" + price_text + ")")

examples/cdp_mode/raw_mobile_async.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async def main():
2525
driver.stop()
2626

2727
if __name__ == "__main__":
28-
# Call an async function with awaited methods
2928
loop = asyncio.new_event_loop()
3029
with decorators.print_runtime("raw_mobile_async.py"):
3130
loop.run_until_complete(main())

examples/cdp_mode/raw_priceline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from seleniumbase import SB
22

3-
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", incognito=True) as sb:
44
url = "https://www.priceline.com"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)
77
sb.click('input[name="endLocation"]')
88
sb.sleep(1.2)
9-
location = "Portland, Oregon, US"
9+
location = "Portland, OR"
1010
selection = "Oregon, United States" # (Dropdown option)
1111
sb.press_keys('input[name="endLocation"]', location)
1212
sb.sleep(1.5)

examples/cdp_mode/raw_softpedia.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, ad_block=True) as sb:
4+
url = "https://www.softpedia.com/"
5+
sb.activate_cdp_mode(url)
6+
search_box = 'input[name="search_term"]'
7+
search = "3D Model Lab"
8+
sb.click(search_box)
9+
sb.press_keys(search_box, search + "\n")
10+
sb.sleep(2)
11+
sb.remove_elements("#adcontainer1")
12+
sb.sleep(2.5)
13+
print('*** Softpedia Search for "%s":' % search)
14+
links = []
15+
item_container = 'div[style="min-height:100px;"]'
16+
sb.wait_for_element(item_container)
17+
items = sb.find_elements(item_container)
18+
for item in items:
19+
result = item.querySelector("h4 a")
20+
links.append(result.get_attribute("href"))
21+
print("* " + result.text)
22+
print(item.querySelector("p").get_attribute("title"))
23+
for link in links:
24+
sb.open(link)
25+
sb.remove_elements("div.ad")
26+
sb.sleep(2)

0 commit comments

Comments
 (0)