Skip to content

Commit 10b83ac

Browse files
committed
Update CDP Mode examples
1 parent cc1073a commit 10b83ac

File tree

7 files changed

+54
-21
lines changed

7 files changed

+54
-21
lines changed

examples/cdp_mode/raw_async.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ async def main():
5151
location = "Amsterdam"
5252
where_to = 'div[data-automation*="experiences"] input'
5353
button = 'button[data-automation*="experiences-search"]'
54+
sb.wait_for_text("Where to?")
5455
sb.gui_click_element(where_to)
5556
sb.press_keys(where_to, location)
5657
sb.sleep(1)
5758
sb.gui_click_element(button)
5859
sb.sleep(3)
5960
print(sb.get_title())
6061
print("************")
61-
cards = sb.select_all('h2[data-automation*="product-list-card"]')
62+
cards = sb.select_all('span[data-automation*="product-list-card"]')
6263
for card in cards:
6364
print("* %s" % card.text)

examples/cdp_mode/raw_cdp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def main():
1616
location = "Amsterdam"
1717
where_to = 'div[data-automation*="experiences"] input'
1818
button = 'button[data-automation*="experiences-search"]'
19+
sb.wait_for_text("Where to?")
1920
sb.gui_click_element(where_to)
2021
sb.press_keys(where_to, location)
2122
sb.sleep(1)
@@ -26,7 +27,7 @@ def main():
2627
for i in range(8):
2728
sb.scroll_down(50)
2829
sb.sleep(0.2)
29-
cards = sb.select_all('h2[data-automation*="product-list-card"]')
30+
cards = sb.select_all('span[data-automation*="product-list-card"]')
3031
for card in cards:
3132
print("* %s" % card.text)
3233

examples/cdp_mode/raw_cdp_with_sb.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
location = "Amsterdam"
1515
where_to = 'div[data-automation*="experiences"] input'
1616
button = 'button[data-automation*="experiences-search"]'
17+
sb.wait_for_text("Where to?")
1718
sb.cdp.gui_click_element(where_to)
1819
sb.press_keys(where_to, location)
1920
sb.sleep(1)
@@ -24,6 +25,6 @@
2425
for i in range(8):
2526
sb.cdp.scroll_down(50)
2627
sb.sleep(0.2)
27-
cards = sb.select_all('h2[data-automation*="product-list-card"]')
28+
cards = sb.select_all('span[data-automation*="product-list-card"]')
2829
for card in cards:
2930
print("* %s" % card.text)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""A script that loads cookies to bypass login."""
2+
import asyncio
3+
import time
4+
from seleniumbase import cdp_driver
5+
6+
7+
# Log in to Swag Labs and save cookies
8+
async def get_login_cookies():
9+
url = "https://www.saucedemo.com"
10+
driver = await cdp_driver.start_async(incognito=True)
11+
page = await driver.get(url)
12+
element = await page.select("#user-name")
13+
await element.send_keys_async("standard_user")
14+
element = await page.select("#password")
15+
await element.send_keys_async("secret_sauce")
16+
element = await page.select('input[type="submit"]')
17+
await element.click_async()
18+
cookies = await driver.cookies.get_all()
19+
await page.close()
20+
return cookies
21+
22+
23+
# Load previously saved cookies to bypass login
24+
async def login_with_cookies(cookies):
25+
url_1 = "https://www.saucedemo.com"
26+
url_2 = "https://www.saucedemo.com/inventory.html"
27+
driver = await cdp_driver.start_async()
28+
page = await driver.get(url_1)
29+
await driver.cookies.set_all(cookies)
30+
await driver.get(url_2)
31+
await page.select("div.inventory_list")
32+
time.sleep(2)
33+
34+
35+
if __name__ == "__main__":
36+
loop = asyncio.new_event_loop()
37+
cookies = loop.run_until_complete(get_login_cookies())
38+
loop.run_until_complete(login_with_cookies(cookies))

examples/cdp_mode/raw_priceline.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from seleniumbase import SB
22

33
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
4-
window_handle = sb.driver.current_window_handle
54
url = "https://www.priceline.com"
65
sb.activate_cdp_mode(url)
76
sb.sleep(2.5)
@@ -17,17 +16,14 @@
1716
sb.sleep(1.5)
1817
sb.cdp.click('button[aria-label="Dismiss calendar"]')
1918
sb.sleep(4.5)
20-
sb.connect()
21-
if len(sb.driver.window_handles) > 1:
22-
sb.switch_to_window(window_handle)
23-
sb.driver.close()
24-
sb.sleep(0.2)
25-
sb.switch_to_newest_window()
19+
if len(sb.cdp.get_tabs()) > 1:
20+
sb.cdp.close_active_tab()
21+
sb.cdp.switch_to_newest_tab()
2622
sb.sleep(0.6)
2723
sb.sleep(0.8)
2824
for y in range(1, 9):
2925
sb.scroll_to_y(y * 400)
30-
sb.sleep(0.75)
26+
sb.sleep(0.5)
3127
hotel_names = sb.find_elements('a[data-autobot-element-id*="HOTEL_NAME"]')
3228
hotel_prices = sb.find_elements('span[font-size="4,,,5"]')
3329
print("Priceline Hotels in %s:" % location)

examples/presenter/uc_presentation_4.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,6 @@ def test_presentation_4(self):
770770
self.begin_presentation(filename="uc_presentation.html")
771771

772772
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
773-
window_handle = sb.driver.current_window_handle
774773
url = "https://www.priceline.com"
775774
sb.activate_cdp_mode(url)
776775
sb.sleep(2.5)
@@ -786,22 +785,19 @@ def test_presentation_4(self):
786785
sb.sleep(1.5)
787786
sb.cdp.click('button[aria-label="Dismiss calendar"]')
788787
sb.sleep(4.5)
789-
sb.connect()
790-
if len(sb.driver.window_handles) > 1:
791-
sb.switch_to_window(window_handle)
792-
sb.driver.close()
793-
sb.sleep(0.2)
794-
sb.switch_to_newest_window()
788+
if len(sb.cdp.get_tabs()) > 1:
789+
sb.cdp.close_active_tab()
790+
sb.cdp.switch_to_newest_tab()
795791
sb.sleep(0.6)
796792
sb.sleep(0.8)
797793
for y in range(1, 9):
798794
sb.scroll_to_y(y * 400)
799-
sb.sleep(0.75)
795+
sb.sleep(0.5)
800796
hotel_names = sb.find_elements(
801797
'a[data-autobot-element-id*="HOTEL_NAME"]'
802798
)
803799
hotel_prices = sb.find_elements('span[font-size="4,,,5"]')
804-
print("\n\nPriceline Hotels in %s:" % location)
800+
print("Priceline Hotels in %s:" % location)
805801
print(sb.get_text('[data-testid="POPOVER-DATE-PICKER"]'))
806802
if len(hotel_names) == 0:
807803
print("No availability over the selected dates!")

help_docs/syntax_formats.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ def main():
10721072
for i in range(8):
10731073
sb.scroll_down(50)
10741074
sb.sleep(0.2)
1075-
cards = sb.select_all('h2[data-automation*="product-list-card"]')
1075+
cards = sb.select_all('span[data-automation*="product-list-card"]')
10761076
for card in cards:
10771077
print("* %s" % card.text)
10781078

0 commit comments

Comments
 (0)