Skip to content

Commit aa0b70a

Browse files
authored
Merge pull request #3573 from seleniumbase/cdp-mode-patch-38
CDP Mode: Patch 38
2 parents 5d732a4 + 2024056 commit aa0b70a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+188
-162
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ with SB(test=True, uc=True) as sb:
9090
```python
9191
from seleniumbase import SB
9292

93-
with SB(uc=True, test=True, locale_code="en") as sb:
93+
with SB(uc=True, test=True, locale="en") as sb:
9494
url = "https://gitlab.com/users/sign_in"
9595
sb.activate_cdp_mode(url)
9696
sb.uc_gui_click_captcha()

examples/cdp_mode/ReadMe.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Simple example: ([SeleniumBase/examples/cdp_mode/raw_gitlab.py](https://github.c
5050
```python
5151
from seleniumbase import SB
5252

53-
with SB(uc=True, test=True, locale_code="en") as sb:
53+
with SB(uc=True, test=True, locale="en") as sb:
5454
url = "https://gitlab.com/users/sign_in"
5555
sb.activate_cdp_mode(url)
5656
sb.uc_gui_click_captcha()
@@ -130,7 +130,7 @@ To find out if WebDriver is connected or disconnected, call:
130130
```python
131131
from seleniumbase import SB
132132

133-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
133+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
134134
url = "https://www.pokemon.com/us"
135135
sb.activate_cdp_mode(url)
136136
sb.sleep(3.2)
@@ -189,7 +189,7 @@ with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
189189
```python
190190
from seleniumbase import SB
191191

192-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
192+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
193193
url = "https://www.hyatt.com/"
194194
sb.activate_cdp_mode(url)
195195
sb.sleep(2.5)
@@ -236,7 +236,7 @@ with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
236236
```python
237237
from seleniumbase import SB
238238

239-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
239+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
240240
url = "https://www.bestwestern.com/en_US.html"
241241
sb.activate_cdp_mode(url)
242242
sb.sleep(2.5)
@@ -328,11 +328,11 @@ with SB(uc=True, test=True, ad_block=True) as sb:
328328
```python
329329
from seleniumbase import SB
330330

331-
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
331+
with SB(uc=True, test=True, locale="en", pls="none") as sb:
332332
url = "https://www.nike.com/"
333333
sb.activate_cdp_mode(url)
334334
sb.sleep(2.5)
335-
sb.cdp.mouse_click('div[data-testid="user-tools-container"]')
335+
sb.cdp.click('div[data-testid="user-tools-container"]')
336336
sb.sleep(1.5)
337337
search = "Nike Air Force 1"
338338
sb.cdp.press_keys('input[type="search"]', search)

examples/cdp_mode/raw_ahrefs.py

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

3-
with SB(uc=True, test=True, incognito=True, locale_code="en") as sb:
3+
with SB(uc=True, test=True, incognito=True, locale="en") as sb:
44
url = "https://ahrefs.com/website-authority-checker"
55
input_field = 'input[placeholder="Enter domain"]'
66
submit_button = 'span:contains("Check Authority")'

examples/cdp_mode/raw_albertsons.py

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

3-
with SB(uc=True, test=True, locale_code="en") as sb:
3+
with SB(uc=True, test=True, locale="en") as sb:
44
url = "https://www.albertsons.com/recipes/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_async.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import asyncio
22
import time
33
from contextlib import suppress
4-
from seleniumbase.core import sb_cdp
5-
from seleniumbase.undetected import cdp_driver
4+
from seleniumbase import sb_cdp
5+
from seleniumbase import cdp_driver
66

77

88
async def main():
9-
driver = await cdp_driver.cdp_util.start_async()
10-
page = await driver.get("about:blank")
11-
await page.set_locale("en")
12-
await page.get("https://www.priceline.com/")
9+
url = "https://www.priceline.com/"
10+
driver = await cdp_driver.start_async(lang="en")
11+
page = await driver.get(url)
1312
time.sleep(3)
1413
print(await page.evaluate("document.title"))
1514
element = await page.select('[data-testid*="endLocation"]')
@@ -24,7 +23,7 @@ async def main():
2423
loop.run_until_complete(main())
2524

2625
# Call everything without using async / await
27-
driver = cdp_driver.cdp_util.start_sync()
26+
driver = cdp_driver.start_sync()
2827
page = loop.run_until_complete(driver.get("about:blank"))
2928
loop.run_until_complete(page.set_locale("en"))
3029
loop.run_until_complete(page.get("https://www.pokemon.com/us"))
@@ -41,11 +40,8 @@ async def main():
4140
print(loop.run_until_complete(page.evaluate("document.title")))
4241
time.sleep(1)
4342

44-
# Call CDP methods via the simplified CDP API
45-
page = loop.run_until_complete(driver.get("about:blank"))
46-
sb = sb_cdp.CDPMethods(loop, page, driver)
47-
sb.set_locale("en")
48-
sb.open("https://www.priceline.com/")
43+
# Call CDP methods via the simplified SB CDP API
44+
sb = sb_cdp.Chrome("https://www.priceline.com/")
4945
sb.sleep(2.5)
5046
sb.internalize_links() # Don't open links in a new tab
5147
sb.click("#link_header_nav_experiences")

examples/cdp_mode/raw_bestwestern.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.bestwestern.com/en_US.html"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_cdp.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
"""Example of using CDP Mode without WebDriver"""
2-
import asyncio
32
from seleniumbase import decorators
4-
from seleniumbase.core import sb_cdp
5-
from seleniumbase.undetected import cdp_driver
3+
from seleniumbase import sb_cdp
64

75

86
@decorators.print_runtime("CDP Priceline Example")
97
def main():
10-
url0 = "about:blank" # Set Locale code from here first
11-
url1 = "https://www.priceline.com/" # (The "real" URL)
12-
loop = asyncio.new_event_loop()
13-
driver = cdp_driver.cdp_util.start_sync()
14-
page = loop.run_until_complete(driver.get(url0))
15-
sb = sb_cdp.CDPMethods(loop, page, driver)
16-
sb.set_locale("en") # This test expects English locale
17-
sb.open(url1)
8+
url = "https://www.priceline.com/"
9+
sb = sb_cdp.Chrome(url, lang="en")
1810
sb.sleep(2.5)
1911
sb.internalize_links() # Don't open links in a new tab
2012
sb.click("#link_header_nav_experiences")

examples/cdp_mode/raw_cdp_extended.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""The long way of using CDP Mode without WebDriver"""
2+
import asyncio
3+
from seleniumbase import sb_cdp
4+
from seleniumbase import cdp_driver
5+
6+
url = "https://seleniumbase.io/demo_page"
7+
loop = asyncio.new_event_loop()
8+
driver = cdp_driver.start_sync()
9+
page = loop.run_until_complete(driver.get(url))
10+
sb = sb_cdp.CDPMethods(loop, page, driver)
11+
12+
sb.press_keys("input", "Text")
13+
sb.highlight("button")
14+
sb.type("textarea", "Here are some words")
15+
sb.click("button")
16+
sb.set_value("input#mySlider", "100")
17+
sb.click_visible_elements("input.checkBoxClassB")
18+
sb.select_option_by_text("#mySelect", "Set to 75%")
19+
sb.gui_hover_and_click("#myDropdown", "#dropOption2")
20+
sb.gui_click_element("#checkBox1")
21+
sb.gui_drag_and_drop("img#logo", "div#drop2")
22+
sb.nested_click("iframe#myFrame3", ".fBox")
23+
sb.sleep(2)

examples/cdp_mode/raw_cdp_methods.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import asyncio
2-
from seleniumbase.core import sb_cdp
3-
from seleniumbase.undetected import cdp_driver
1+
from seleniumbase import sb_cdp
42

53
url = "https://seleniumbase.io/demo_page"
6-
loop = asyncio.new_event_loop()
7-
driver = cdp_driver.cdp_util.start_sync()
8-
page = loop.run_until_complete(driver.get(url))
9-
sb = sb_cdp.CDPMethods(loop, page, driver)
10-
4+
sb = sb_cdp.Chrome(url)
115
sb.press_keys("input", "Text")
126
sb.highlight("button")
137
sb.type("textarea", "Here are some words")

examples/cdp_mode/raw_cdp_nike.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import asyncio
2-
from seleniumbase.core import sb_cdp
3-
from seleniumbase.undetected import cdp_driver
1+
from seleniumbase import sb_cdp
42

53
url = "https://www.nike.com/"
6-
loop = asyncio.new_event_loop()
7-
driver = cdp_driver.cdp_util.start_sync()
8-
page = loop.run_until_complete(driver.get(url))
9-
sb = sb_cdp.CDPMethods(loop, page, driver)
10-
11-
search = "Road Racing Shoes"
4+
sb = sb_cdp.Chrome(url)
125
sb.click('div[data-testid="user-tools-container"]')
136
sb.sleep(1)
7+
search = "Road Racing Shoes"
148
sb.press_keys('input[type="search"]', search)
159
sb.sleep(4)
16-
1710
elements = sb.select_all('ul[data-testid*="products"] figure .details')
1811
if elements:
1912
print('**** Found results for "%s": ****' % search)

examples/cdp_mode/raw_cdp_with_sb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from seleniumbase import SB
33

44

5-
with SB(uc=True, test=True, locale_code="en") as sb:
5+
with SB(uc=True, test=True, locale="en") as sb:
66
url = "https://www.priceline.com/"
77
sb.activate_cdp_mode(url)
88
sb.sleep(2.5)

examples/cdp_mode/raw_cf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Using CDP Mode with PyAutoGUI to bypass CAPTCHAs."""
22
from seleniumbase import SB
33

4-
with SB(uc=True, test=True, locale_code="en", guest=True) as sb:
4+
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)
77
sb.sleep(3)
88
sb.uc_gui_handle_captcha() # PyAutoGUI press Tab and Spacebar
99
sb.sleep(2)
1010

11-
with SB(uc=True, test=True, locale_code="en", guest=True) as sb:
11+
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(3)

examples/cdp_mode/raw_easyjet.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.easyjet.com/en/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_elal.py

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

3-
with SB(uc=True, test=True, locale_code="en") as sb:
3+
with SB(uc=True, test=True, locale="en") as sb:
44
url = "www.elal.com/flight-deals/en-us/flights-from-boston-to-tel-aviv"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2)
@@ -36,19 +36,21 @@
3636
sb.cdp.click(search_cell)
3737
sb.sleep(5)
3838
else:
39-
elements = sb.cdp.find_elements("div.ui-bound__price__value")
4039
print("*** Lowest Prices: ***")
41-
first = True
40+
departure_prices = "#uiFlightPanel0 div.ui-bound__price__value"
41+
return_prices = "#uiFlightPanel1 div.ui-bound__price__value"
42+
elements = sb.cdp.find_elements(departure_prices)
4243
for element in elements:
4344
if "lowest price" in element.text:
44-
if first:
45-
print("Departure Flight:")
46-
print(element.text)
47-
first = False
48-
else:
49-
print("Return Flight:")
50-
print(element.text)
51-
break
45+
print("Departure Flight:")
46+
print(element.text)
47+
break
48+
elements = sb.cdp.find_elements(return_prices)
49+
for element in elements:
50+
if "lowest price" in element.text:
51+
print("Return Flight:")
52+
print(element.text)
53+
break
5254
dates = sb.cdp.find_elements('div[class*="flight-date"]')
5355
if len(dates) == 2:
5456
print("*** Departure Date: ***")

examples/cdp_mode/raw_footlocker.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.footlocker.com/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_gettyimages.py

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

3-
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
3+
with SB(uc=True, test=True, locale="en", pls="none") as sb:
44
sb.activate_cdp_mode("https://www.gettyimages.com/")
55
sb.cdp.click('label:contains("Editorial")')
66
sb.cdp.press_keys("form input", "comic con 2024 sci fi panel\n")

examples/cdp_mode/raw_gitlab.py

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

3-
with SB(uc=True, test=True, locale_code="en") as sb:
3+
with SB(uc=True, test=True, locale="en") as sb:
44
url = "https://gitlab.com/users/sign_in"
55
sb.activate_cdp_mode(url)
66
sb.uc_gui_click_captcha()

examples/cdp_mode/raw_hyatt.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.hyatt.com/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_kohls.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.kohls.com/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)

examples/cdp_mode/raw_multi_async.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import asyncio
33
from concurrent.futures import ThreadPoolExecutor
44
from random import randint
5-
from seleniumbase.undetected import cdp_driver
5+
from seleniumbase import cdp_driver
66

77

88
async def main(url):
9-
driver = await cdp_driver.cdp_util.start_async()
9+
driver = await cdp_driver.start_async()
1010
page = await driver.get(url)
1111
await page.set_window_rect(randint(4, 600), randint(8, 410), 860, 500)
1212
await page.sleep(0.5)

examples/cdp_mode/raw_multi_cdp.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# Testing multiple CDP drivers using the sync API
2-
import asyncio
32
from concurrent.futures import ThreadPoolExecutor
43
from random import randint
5-
from seleniumbase.core import sb_cdp
6-
from seleniumbase.undetected import cdp_driver
4+
from seleniumbase import sb_cdp
75

86

97
def main(url):
10-
loop = asyncio.new_event_loop()
11-
driver = cdp_driver.cdp_util.start_sync()
12-
page = loop.run_until_complete(driver.get(url))
13-
sb = sb_cdp.CDPMethods(loop, page, driver)
8+
sb = sb_cdp.Chrome(url)
149
sb.set_window_rect(randint(4, 720), randint(8, 410), 800, 500)
1510
sb.press_keys("input", "Text")
1611
sb.highlight("button")

examples/cdp_mode/raw_nike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from seleniumbase import SB
22

3-
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
3+
with SB(uc=True, test=True, locale="en", pls="none") as sb:
44
url = "https://www.nike.com/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)
7-
sb.cdp.mouse_click('div[data-testid="user-tools-container"]')
7+
sb.cdp.click('div[data-testid="user-tools-container"]')
88
sb.sleep(1.5)
99
search = "Nike Air Force 1"
1010
sb.cdp.press_keys('input[type="search"]', search)

examples/cdp_mode/raw_nordstrom.py

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

3-
with SB(uc=True, test=True, locale_code="en") as sb:
3+
with SB(uc=True, test=True, locale="en") as sb:
44
url = "https://www.nordstrom.com/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.2)

examples/cdp_mode/raw_pokemon.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.pokemon.com/us"
55
sb.activate_cdp_mode(url)
66
sb.sleep(3.2)

examples/cdp_mode/raw_priceline.py

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

3-
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
3+
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
window_handle = sb.driver.current_window_handle
55
url = "https://www.priceline.com"
66
sb.activate_cdp_mode(url)

0 commit comments

Comments
 (0)