Skip to content

Commit 58626be

Browse files
authored
Merge pull request #4166 from seleniumbase/cdp-mode-patch-84
CDP Mode: Patch 84
2 parents 0a196bd + c47b451 commit 58626be

File tree

20 files changed

+201
-44
lines changed

20 files changed

+201
-44
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from playwright.sync_api import sync_playwright
2+
from seleniumbase import sb_cdp
3+
4+
sb = sb_cdp.Chrome(locale="en", ad_block=True)
5+
endpoint_url = sb.get_endpoint_url()
6+
7+
with sync_playwright() as p:
8+
browser = p.chromium.connect_over_cdp(endpoint_url)
9+
context = browser.contexts[0]
10+
page = context.pages[0]
11+
page.goto("https://seatgeek.com/")
12+
input_field = 'input[name="search"]'
13+
page.wait_for_selector(input_field)
14+
sb.sleep(1.6)
15+
query = "Jerry Seinfeld"
16+
sb.press_keys(input_field, query)
17+
sb.sleep(1.6)
18+
page.click("li#active-result-item")
19+
sb.sleep(4.2)
20+
print('*** SeatGeek Search for "%s":' % query)
21+
items = page.locator('[data-testid="listing-item"]')
22+
for i in range(items.count()):
23+
item_text = items.nth(i).inner_text()
24+
print(item_text.replace("\n\n", "\n"))

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)

0 commit comments

Comments
 (0)