Skip to content

Commit 6ab045c

Browse files
committed
Update CDP Mode examples
1 parent abeeb4a commit 6ab045c

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import mycdp
2+
from seleniumbase import sb_cdp
3+
4+
sb = sb_cdp.Chrome()
5+
tab = sb.get_active_tab()
6+
loop = sb.get_event_loop()
7+
loop.run_until_complete(
8+
tab.send(
9+
mycdp.emulation.set_device_metrics_override(
10+
width=411, height=731, device_scale_factor=3, mobile=True
11+
)
12+
)
13+
)
14+
url = "https://gitlab.com/users/sign_in"
15+
sb.open(url)
16+
sb.sleep(2)
17+
sb.solve_captcha()
18+
# (The rest is for testing and demo purposes)
19+
sb.assert_text("Username", '[for="user_login"]', timeout=3)
20+
sb.assert_element('label[for="user_login"]')
21+
sb.highlight('button:contains("Sign in")')
22+
sb.highlight('h1:contains("GitLab")')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import asyncio
2+
import mycdp
3+
import time
4+
from seleniumbase import cdp_driver
5+
from seleniumbase import decorators
6+
7+
8+
async def main():
9+
url = "https://gitlab.com/users/sign_in"
10+
driver = await cdp_driver.start_async()
11+
await driver.main_tab.send(
12+
mycdp.emulation.set_device_metrics_override(
13+
width=411, height=731, device_scale_factor=3, mobile=True
14+
)
15+
)
16+
page = await driver.get(url, lang="en")
17+
time.sleep(3)
18+
try:
19+
element = await page.select('[style*="grid"] div div', timeout=1)
20+
await element.mouse_click_with_offset_async(32, 28)
21+
except Exception:
22+
pass # Maybe CAPTCHA was already bypassed
23+
element = await page.select('label[for="user_login"]')
24+
await element.flash_async(duration=1.5, color="44EE44")
25+
time.sleep(1)
26+
element = await page.select('[data-testid="sign-in-button"]')
27+
await element.flash_async(duration=2, color="44EE44")
28+
time.sleep(2)
29+
30+
if __name__ == "__main__":
31+
# Call an async function with awaited methods
32+
loop = asyncio.new_event_loop()
33+
with decorators.print_runtime("raw_mobile_async.py"):
34+
loop.run_until_complete(main())
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import mycdp
2+
from seleniumbase import SB
3+
4+
with SB(uc=True, test=True) as sb:
5+
url = "https://gitlab.com/users/sign_in"
6+
sb.activate_cdp_mode()
7+
tab = sb.cdp.get_active_tab()
8+
loop = sb.cdp.get_event_loop()
9+
loop.run_until_complete(
10+
tab.send(
11+
mycdp.emulation.set_device_metrics_override(
12+
width=411, height=731, device_scale_factor=3, mobile=True
13+
)
14+
)
15+
)
16+
sb.open(url)
17+
sb.sleep(2)
18+
sb.solve_captcha()
19+
# (The rest is for testing and demo purposes)
20+
sb.assert_text("Username", '[for="user_login"]', timeout=3)
21+
sb.assert_element('label[for="user_login"]')
22+
sb.highlight('button:contains("Sign in")')
23+
sb.highlight('h1:contains("GitLab")')
24+
sb.post_message("SeleniumBase wasn't detected", duration=4)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import mycdp
2+
from seleniumbase import SB
3+
4+
with SB(uc=True, test=True) as sb:
5+
url = "https://www.roblox.com/"
6+
agent = (
7+
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 "
8+
"(KHTML, like Gecko) Mobile Safari/537.36"
9+
)
10+
sb.activate_cdp_mode(agent=agent)
11+
tab = sb.cdp.get_active_tab()
12+
loop = sb.cdp.get_event_loop()
13+
loop.run_until_complete(
14+
tab.send(
15+
mycdp.emulation.set_device_metrics_override(
16+
width=411, height=731, device_scale_factor=3, mobile=True
17+
)
18+
)
19+
)
20+
sb.open(url)
21+
sb.assert_element("#download-the-app-container")
22+
sb.assert_text("Roblox for Android")
23+
sb.highlight('span:contains("Roblox for Android")', loops=8)
24+
sb.highlight('span:contains("Continue in App")', loops=8)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Test Stop & Shop search. Non-US IPs might be blocked."""
2+
from seleniumbase import SB
3+
4+
with SB(uc=True, test=True, guest=True, ad_block=True) as sb:
5+
url = "https://stopandshop.com/"
6+
sb.activate_cdp_mode(url)
7+
sb.sleep(2.6)
8+
if not sb.is_element_present("#brand-logo_link"):
9+
sb.refresh()
10+
sb.sleep(2.6)
11+
sb.wait_for_element("#brand-logo_link", timeout=3)
12+
query = "Fresh Turkey"
13+
required_text = "Turkey"
14+
search_box = 'input[type="search"]'
15+
sb.wait_for_element(search_box)
16+
sb.sleep(1.2)
17+
sb.press_keys(search_box, query)
18+
sb.sleep(1.2)
19+
sb.click("button.search-btn")
20+
sb.sleep(3.2)
21+
print('*** Stop & Shop Search for "%s":' % query)
22+
print(' (Results must contain "%s".)' % required_text)
23+
print(' (Results cannot contain "Out of Stock")')
24+
unique_item_text = []
25+
item_selector = "div.product-tile_content"
26+
items = sb.find_elements(item_selector)
27+
for item in items:
28+
sb.sleep(0.06)
29+
if "Out of Stock" not in item.text:
30+
if required_text in item.text:
31+
item.flash(color="44CC88")
32+
sb.sleep(0.025)
33+
if item.text not in unique_item_text:
34+
unique_item_text.append(item.text)
35+
print("* " + item.text)

0 commit comments

Comments
 (0)