Skip to content

Commit 5bde4aa

Browse files
authored
Merge pull request #3481 from seleniumbase/cdp-mode-patch-29
CDP Mode - Patch 29
2 parents 4ba4d68 + 3e3fec9 commit 5bde4aa

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

examples/cdp_mode/raw_multi_async.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Testing multiple CDP drivers using the async API
2+
import asyncio
3+
from concurrent.futures import ThreadPoolExecutor
4+
from random import randint
5+
from seleniumbase.undetected import cdp_driver
6+
7+
8+
async def main(url):
9+
driver = await cdp_driver.cdp_util.start_async()
10+
page = await driver.get(url)
11+
await page.set_window_rect(randint(4, 600), randint(8, 410), 860, 500)
12+
await page.sleep(0.5)
13+
field = await page.select("input")
14+
await field.send_keys_async("Text")
15+
button = await page.select("button")
16+
await button.click_async()
17+
await page.sleep(2)
18+
19+
20+
def set_up_loop(url):
21+
loop = asyncio.new_event_loop()
22+
loop.run_until_complete(main(url))
23+
24+
25+
if __name__ == "__main__":
26+
urls = ["https://seleniumbase.io/demo_page" for i in range(4)]
27+
with ThreadPoolExecutor(max_workers=len(urls)) as executor:
28+
for url in urls:
29+
executor.submit(set_up_loop, url)

examples/cdp_mode/raw_multi_cdp.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Testing multiple CDP drivers using the sync API
2+
import asyncio
3+
from concurrent.futures import ThreadPoolExecutor
4+
from random import randint
5+
from seleniumbase.core import sb_cdp
6+
from seleniumbase.undetected import cdp_driver
7+
8+
9+
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)
14+
sb.set_window_rect(randint(4, 720), randint(8, 410), 800, 500)
15+
sb.press_keys("input", "Text")
16+
sb.highlight("button")
17+
sb.click("button")
18+
sb.sleep(2)
19+
20+
21+
if __name__ == "__main__":
22+
urls = ["https://seleniumbase.io/demo_page" for i in range(4)]
23+
with ThreadPoolExecutor(max_workers=len(urls)) as executor:
24+
for url in urls:
25+
executor.submit(main, url)

seleniumbase/__version__.py

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

seleniumbase/core/sb_cdp.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,13 @@ def set_value(self, selector, text, timeout=None):
877877

878878
def evaluate(self, expression):
879879
"""Run a JavaScript expression and return the result."""
880-
if expression.startswith("return "):
881-
expression = expression[len("return "):]
880+
expression = expression.strip()
881+
exp_list = expression.split("\n")
882+
if exp_list and exp_list[-1].strip().startswith("return "):
883+
expression = (
884+
"\n".join(exp_list[0:-1]) + "\n"
885+
+ exp_list[-1].strip()[len("return "):]
886+
).strip()
882887
return self.loop.run_until_complete(
883888
self.page.evaluate(expression)
884889
)

seleniumbase/undetected/cdp_driver/cdp_util.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,18 @@ async def start_async(*args, **kwargs) -> Browser:
241241

242242

243243
def start_sync(*args, **kwargs) -> Browser:
244-
loop = asyncio.get_event_loop()
244+
loop = None
245+
if (
246+
"loop" in kwargs
247+
and kwargs["loop"]
248+
and hasattr(kwargs["loop"], "create_task")
249+
):
250+
loop = kwargs["loop"]
251+
else:
252+
try:
253+
loop = asyncio.get_event_loop()
254+
except RuntimeError:
255+
loop = asyncio.new_event_loop()
245256
headless = False
246257
binary_location = None
247258
if "browser_executable_path" in kwargs:

seleniumbase/undetected/cdp_driver/tab.py

+4
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,10 @@ async def set_window_size(self, left=0, top=0, width=1280, height=1024):
896896
"""
897897
return await self.set_window_state(left, top, width, height)
898898

899+
async def set_window_rect(self, left=0, top=0, width=1280, height=1024):
900+
"""Same as set_window_size(). Uses a different naming convention."""
901+
return await self.set_window_state(left, top, width, height)
902+
899903
async def activate(self):
900904
"""Active this target (Eg: tab, window, page)"""
901905
await self.send(cdp.target.activate_target(self.target.target_id))

0 commit comments

Comments
 (0)