Skip to content

Commit 94b3fa6

Browse files
authored
Merge pull request #3693 from seleniumbase/improve-cdp-multithreading
Improve CDP Multithreading
2 parents 395e6a0 + 2d5c810 commit 94b3fa6

File tree

10 files changed

+47
-21
lines changed

10 files changed

+47
-21
lines changed

examples/cdp_mode/ReadMe.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ with SB(uc=True, test=True, ad_block=True) as sb:
286286
sb.activate_cdp_mode(url)
287287
sb.sleep(2.5)
288288
sb.cdp.click_if_visible('[data-automation-id*="close-mark"]')
289+
sb.sleep(0.3)
289290
sb.cdp.mouse_click('input[aria-label="Search"]')
290291
sb.sleep(1.2)
291292
search = "Settlers of Catan Board Game"
@@ -300,7 +301,7 @@ with SB(uc=True, test=True, ad_block=True) as sb:
300301
for item in items:
301302
if required_text in item.text:
302303
description = item.querySelector(
303-
'[data-automation-id="product-price"] + span'
304+
'[data-automation-id="product-title"]'
304305
)
305306
if description and description.text not in unique_item_text:
306307
unique_item_text.append(description.text)

examples/cdp_mode/raw_pixelscan.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
with SB(uc=True, incognito=True, test=True) as sb:
44
sb.activate_cdp_mode("https://pixelscan.net/")
5-
sb.sleep(3)
6-
sb.remove_elements(".bg-bannerBg") # Remove the top banner
7-
sb.remove_elements("pxlscn-ad1") # Remove the ad banner
5+
sb.sleep(2)
6+
sb.click('button[class*="startButton"]')
7+
sb.sleep(6)
8+
sb.remove_elements(".bg-bannerBg") # Remove top banner
9+
sb.remove_elements("pxlscn-ad1") # Remove an ad banner
10+
sb.remove_elements("pxlscn-ad2") # Remove an ad banner
811
sb.remove_elements("jdiv") # Remove chat widgets
12+
sb.sleep(14)
913
not_masking_text = "You are not masking your fingerprint"
10-
sb.assert_text(not_masking_text, "pxlscn-fingerprint-masking")
14+
sb.assert_text(
15+
not_masking_text,
16+
"pxlscn-fingerprint-masking",
17+
timeout=20,
18+
)
1119
no_automation_detected = "No automation framework detected"
1220
sb.assert_text(no_automation_detected, "pxlscn-bot-detection")
1321
consistent_selector = 'div.bg-consistentBg [alt="Good"]'

examples/cdp_mode/raw_theaters.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
url = "https://architectureofcities.com/roman-theaters"
66
sb.activate_cdp_mode(url)
77
sb.cdp.click_if_visible("#cn-close-notice")
8+
sb.cdp.click_if_visible('span:contains("Continue")')
89
sb.sleep(1)
910
print("*** " + sb.cdp.get_text("h1") + " ***")
1011
for item in sb.cdp.find_elements("h3"):

examples/cdp_mode/raw_walmart.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
sb.activate_cdp_mode(url)
66
sb.sleep(2.5)
77
sb.cdp.click_if_visible('[data-automation-id*="close-mark"]')
8+
sb.sleep(0.3)
89
sb.cdp.mouse_click('input[aria-label="Search"]')
910
sb.sleep(1.2)
1011
search = "Settlers of Catan Board Game"
@@ -19,7 +20,7 @@
1920
for item in items:
2021
if required_text in item.text:
2122
description = item.querySelector(
22-
'[data-automation-id="product-price"] + span'
23+
'[data-automation-id="product-title"]'
2324
)
2425
if description and description.text not in unique_item_text:
2526
unique_item_text.append(description.text)

examples/presenter/uc_presentation_4.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ def test_presentation_4(self):
521521
sb.activate_cdp_mode(url)
522522
sb.sleep(2.5)
523523
sb.cdp.click_if_visible('[data-automation-id*="close-mark"]')
524+
sb.sleep(0.3)
524525
sb.cdp.mouse_click('input[aria-label="Search"]')
525526
sb.sleep(1.2)
526527
search = "Settlers of Catan Board Game"
@@ -535,7 +536,7 @@ def test_presentation_4(self):
535536
for item in items:
536537
if required_text in item.text:
537538
description = item.querySelector(
538-
'[data-automation-id="product-price"] + span'
539+
'[data-automation-id="product-title"]'
539540
)
540541
if (
541542
description

examples/raw_cdp_logging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
url = "seleniumbase.io/apps/turnstile"
77
driver.uc_open_with_reconnect(url, 2)
88
driver.uc_gui_handle_captcha()
9-
driver.sleep(3)
9+
driver.sleep(2)
1010
pprint(driver.get_log("performance"))
1111
finally:
1212
driver.quit()

examples/raw_pixelscan.py

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

33
with SB(uc=True, incognito=True, test=True) as sb:
4-
sb.driver.uc_open_with_reconnect("https://pixelscan.net/", 7)
5-
sb.remove_elements(".bg-bannerBg") # Remove the top banner
6-
sb.remove_elements("pxlscn-ad1") # Remove the ad banner
4+
sb.driver.uc_open_with_reconnect("https://pixelscan.net/", 2)
5+
sb.uc_click('button[class*="startButton"]', reconnect_time=20)
6+
sb.remove_elements(".bg-bannerBg") # Remove top banner
7+
sb.remove_elements("pxlscn-ad1") # Remove an ad banner
8+
sb.remove_elements("pxlscn-ad2") # Remove an ad banner
79
sb.remove_elements("jdiv") # Remove chat widgets
810
no_automation_detected = "No automation framework detected"
9-
sb.assert_text(no_automation_detected, "pxlscn-bot-detection")
11+
sb.assert_text(
12+
no_automation_detected,
13+
"pxlscn-bot-detection",
14+
timeout=20,
15+
)
1016
not_masking_text = "You are not masking your fingerprint"
1117
sb.assert_text(not_masking_text, "pxlscn-fingerprint-masking")
1218
consistent_selector = 'div.bg-consistentBg [alt="Good"]'

seleniumbase/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.37.5"
2+
__version__ = "4.37.6"

seleniumbase/undetected/__init__.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def get(self, url):
402402

403403
def add_cdp_listener(self, event_name, callback):
404404
if (
405-
self.reactor
405+
hasattr(self, "reactor")
406+
and self.reactor
406407
and self.reactor is not None
407408
and isinstance(self.reactor, Reactor)
408409
):
@@ -411,7 +412,11 @@ def add_cdp_listener(self, event_name, callback):
411412
return False
412413

413414
def clear_cdp_listeners(self):
414-
if self.reactor and isinstance(self.reactor, Reactor):
415+
if (
416+
hasattr(self, "reactor")
417+
and self.reactor
418+
and isinstance(self.reactor, Reactor)
419+
):
415420
self.reactor.handlers.clear()
416421

417422
def window_new(self, url=None):
@@ -581,7 +586,11 @@ def quit(self):
581586
finally:
582587
with suppress(Exception):
583588
self.service._terminate_process()
584-
if self.reactor and hasattr(self.reactor, "event"):
589+
if (
590+
hasattr(self, "reactor")
591+
and self.reactor
592+
and hasattr(self.reactor, "event")
593+
):
585594
logger.debug("Shutting down Reactor")
586595
with suppress(Exception):
587596
self.reactor.event.set()

seleniumbase/undetected/cdp_driver/connection.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,16 @@ async def aclose(self):
292292
Closes the websocket connection. Shouldn't be called manually by users.
293293
"""
294294
if self.websocket and self.websocket.state is not State.CLOSED:
295-
if self.listener and self.listener.running:
296-
self.listener.cancel()
297-
self.enabled_domains.clear()
298295
try:
299296
await self.websocket.close()
300297
except Exception:
301298
logger.debug(
302299
"\n❌ Error closing websocket connection to %s",
303300
self.websocket_url
304301
)
302+
if self.listener and self.listener.running:
303+
self.listener.cancel()
304+
self.enabled_domains.clear()
305305
logger.debug(
306306
"\n❌ Closed websocket connection to %s", self.websocket_url
307307
)
@@ -549,8 +549,7 @@ async def listener_loop(self):
549549
except asyncio.TimeoutError:
550550
self.idle.set()
551551
# Pause for a moment.
552-
# await asyncio.sleep(self.time_before_considered_idle / 10)
553-
await asyncio.sleep(0.015)
552+
await asyncio.sleep(self.time_before_considered_idle / 10)
554553
continue
555554
except (Exception,) as e:
556555
logger.debug(

0 commit comments

Comments
 (0)