Skip to content

Commit e56ec82

Browse files
committed
Add new CDP Mode examples
1 parent f242c8d commit e56ec82

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

examples/cdp_mode/raw_easyjet.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en") as sb:
4+
url = "https://www.easyjet.com/en/"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(2.5)
7+
sb.cdp.click_if_visible('button#ensRejectAll')
8+
sb.sleep(1.2)
9+
sb.cdp.click('input[name="from"]')
10+
sb.sleep(1.2)
11+
sb.cdp.type('input[name="from"]', "London")
12+
sb.sleep(1.2)
13+
sb.cdp.click('span[data-testid="airport-name"]')
14+
sb.sleep(1.2)
15+
sb.cdp.type('input[name="to"]', "Venice")
16+
sb.sleep(1.2)
17+
sb.cdp.click('span[data-testid="airport-name"]')
18+
sb.sleep(1.2)
19+
sb.cdp.click('input[name="when"]')
20+
sb.sleep(1.2)
21+
sb.cdp.click('[data-testid="month"] button[aria-disabled="false"]')
22+
sb.sleep(1.2)
23+
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
24+
sb.sleep(1.2)
25+
sb.cdp.click('button[data-testid="submit"]')
26+
sb.sleep(3.5)
27+
sb.connect()
28+
sb.sleep(0.5)
29+
if "easyjet.com" not in sb.get_current_url():
30+
sb.driver.close()
31+
sb.switch_to_newest_window()
32+
days = sb.find_elements("div.flight-grid-day")
33+
for day in days:
34+
print("**** " + " ".join(day.text.split("\n")[0:2]) + " ****")
35+
fares = day.find_elements("css selector", "button.selectable")
36+
if not fares:
37+
print("No flights today!")
38+
for fare in fares:
39+
info = fare.text
40+
info = info.replace("LOWEST FARE\n", "")
41+
info = info.replace("\n", " ")
42+
print(info)

examples/cdp_mode/raw_res_sb.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Using CDP.network.ResponseReceived and CDP.network.RequestWillBeSent."""
2+
import colorama
3+
import mycdp
4+
import sys
5+
from seleniumbase import SB
6+
7+
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
8+
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
9+
cr = colorama.Style.RESET_ALL
10+
if "linux" in sys.platform:
11+
c1 = c2 = cr = ""
12+
13+
14+
async def send_handler(event: mycdp.network.RequestWillBeSent):
15+
r = event.request
16+
s = f"{r.method} {r.url}"
17+
for k, v in r.headers.items():
18+
s += f"\n\t{k} : {v}"
19+
print(c1 + "*** ==> RequestWillBeSent <== ***" + cr)
20+
print(s)
21+
22+
23+
async def receive_handler(event: mycdp.network.ResponseReceived):
24+
print(c2 + "*** ==> ResponseReceived <== ***" + cr)
25+
print(event.response)
26+
27+
28+
with SB(uc=True, test=True, locale_code="en") as sb:
29+
sb.activate_cdp_mode("about:blank")
30+
sb.cdp.add_handler(mycdp.network.RequestWillBeSent, send_handler)
31+
sb.cdp.add_handler(mycdp.network.ResponseReceived, receive_handler)
32+
url = "https://seleniumbase.io/apps/calculator"
33+
sb.cdp.open(url)
34+
sb.sleep(1)

examples/cdp_mode/raw_walmart.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en") as sb:
4+
url = "https://www.walmart.com/"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(2.5)
7+
sb.cdp.mouse_click('input[aria-label="Search"]')
8+
sb.sleep(1.2)
9+
search = "Settlers of Catan Board Game"
10+
required_text = "Catan"
11+
sb.cdp.press_keys('input[aria-label="Search"]', search + "\n")
12+
sb.sleep(3.8)
13+
items = sb.cdp.find_elements('div[data-testid="list-view"]')
14+
print('*** Walmart Search for "%s":' % search)
15+
print(' (Results must contain "%s".)' % required_text)
16+
for item in items:
17+
if required_text in item.text:
18+
description = item.querySelector(
19+
'[data-automation-id="product-price"] + span'
20+
)
21+
if description:
22+
print("* " + description.text)
23+
price = item.querySelector(
24+
'[data-automation-id="product-price"]'
25+
)
26+
if price:
27+
price_text = price.text
28+
price_text = price_text.split("current price Now ")[-1]
29+
price_text = price_text.split("current price ")[-1]
30+
price_text = price_text.split(" ")[0]
31+
print(" (" + price_text + ")")

0 commit comments

Comments
 (0)