Skip to content

Commit 827609b

Browse files
committed
Update the CDP Mode docs
1 parent cf08b2b commit 827609b

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ with SB(uc=True, test=True, locale_code="en") as sb:
150150
'div[data-booking-status="BOOKABLE"] [class*="HotelCard_header"]'
151151
)
152152
hotel_prices = sb.cdp.select_all(
153-
'div[data-booking-status="BOOKABLE"] div.rate-currency'
153+
'div[data-booking-status="BOOKABLE"] div.rate'
154154
)
155155
sb.assert_true(len(hotel_names) == len(hotel_prices))
156156
print("Hyatt Hotels in %s:" % location)
@@ -207,6 +207,82 @@ with SB(uc=True, test=True, locale_code="en") as sb:
207207

208208
</details>
209209

210+
### 🔖 Example 4: (Walmart site using Akamai protection with PerimeterX)
211+
212+
* [SeleniumBase/examples/cdp_mode/raw_walmart.py](https://github.com/seleniumbase/SeleniumBase/tree/master/examples/cdp_mode/raw_walmart.py)
213+
214+
<div></div>
215+
<details>
216+
<summary> ▶️ (<b>Click to expand code preview</b>)</summary>
217+
218+
```python
219+
from seleniumbase import SB
220+
221+
with SB(uc=True, test=True, locale_code="en") as sb:
222+
url = "https://www.walmart.com/"
223+
sb.activate_cdp_mode(url)
224+
sb.sleep(2.5)
225+
sb.cdp.mouse_click('input[aria-label="Search"]')
226+
sb.sleep(1.2)
227+
search = "Settlers of Catan Board Game"
228+
required_text = "Catan"
229+
sb.cdp.press_keys('input[aria-label="Search"]', search + "\n")
230+
sb.sleep(3.8)
231+
print('*** Walmart Search for "%s":' % search)
232+
print(' (Results must contain "%s".)' % required_text)
233+
unique_item_text = []
234+
items = sb.cdp.find_elements('div[data-testid="list-view"]')
235+
for item in items:
236+
if required_text in item.text:
237+
description = item.querySelector(
238+
'[data-automation-id="product-price"] + span'
239+
)
240+
if description and description.text not in unique_item_text:
241+
unique_item_text.append(description.text)
242+
print("* " + description.text)
243+
price = item.querySelector(
244+
'[data-automation-id="product-price"]'
245+
)
246+
if price:
247+
price_text = price.text
248+
price_text = price_text.split("current price Now ")[-1]
249+
price_text = price_text.split("current price ")[-1]
250+
price_text = price_text.split(" ")[0]
251+
print(" (" + price_text + ")")
252+
```
253+
254+
</details>
255+
256+
### 🔖 Example 5: (Nike site using Shape Security)
257+
258+
* [SeleniumBase/examples/cdp_mode/raw_nike.py](https://github.com/seleniumbase/SeleniumBase/tree/master/examples/cdp_mode/raw_nike.py)
259+
260+
<div></div>
261+
<details>
262+
<summary> ▶️ (<b>Click to expand code preview</b>)</summary>
263+
264+
```python
265+
from seleniumbase import SB
266+
267+
with SB(uc=True, test=True, locale_code="en") as sb:
268+
url = "https://www.nike.com/"
269+
sb.activate_cdp_mode(url)
270+
sb.sleep(3)
271+
sb.cdp.gui_click_element('div[data-testid="user-tools-container"]')
272+
sb.sleep(1.5)
273+
search = "Nike Air Force 1"
274+
sb.cdp.press_keys('input[type="search"]', search)
275+
sb.sleep(4)
276+
elements = sb.cdp.select_all('ul[data-testid*="products"] figure .details')
277+
if elements:
278+
print('**** Found results for "%s": ****' % search)
279+
for element in elements:
280+
print("* " + element.text)
281+
sb.sleep(2)
282+
```
283+
284+
</details>
285+
210286
(<b>Note:</b> Extra <code translate="no">sb.sleep()</code> calls have been added to prevent bot-detection because some sites will flag you as a bot if you perform actions too quickly.)
211287

212288
(<b>Note:</b> Some sites may IP-block you for 36 hours or more if they catch you using regular <span translate="no">Selenium WebDriver</span>. Be extra careful when creating and/or modifying automation scripts that run on them.)

0 commit comments

Comments
 (0)