Skip to content

Commit 3911a26

Browse files
authored
Den 231 improve fast selector agent (#39)
* update wait_for to loading for Status * switch to take image before getting html * update timeout for get_element to use increasing intervals
1 parent fb06629 commit 3911a26

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

dendrite_sdk/_common/status.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from typing import Literal
22

33

4-
Status = Literal["success", "failed", "wait_for"]
4+
Status = Literal["success", "failed", "loading"]

dendrite_sdk/_core/dendrite_page.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ async def _get_page_information(self) -> PageInformation:
214214
Returns:
215215
PageInformation: An object containing the page's URL, raw HTML, and a screenshot in base64 format.
216216
"""
217-
soup = await self._get_soup()
218217

219218
base64 = await self.screenshot_manager.take_full_page_screenshot(
220219
self.playwright_page
221220
)
221+
soup = await self._get_soup()
222222

223223
return PageInformation(
224224
url=self.playwright_page.url,

dendrite_sdk/_core/mixin/get_element.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import time
23
from typing import Dict, List, Literal, Optional, Union, overload
34

45
from loguru import logger
@@ -10,6 +11,10 @@
1011
from dendrite_sdk._exceptions.dendrite_exception import DendriteException
1112

1213

14+
# The timeout interval between retries in milliseconds
15+
TIMEOUT_INTERVAL = [150, 450, 900]
16+
17+
1318
class GetElementMixin(DendritePageProtocol):
1419
@overload
1520
async def get_elements(
@@ -240,7 +245,19 @@ async def _get_element(
240245
"""
241246

242247
llm_config = self.dendrite_browser.llm_config
248+
attempt_start = time.time()
243249
for attempt in range(max_retries):
250+
current_timeout = (
251+
TIMEOUT_INTERVAL[attempt]
252+
if len(TIMEOUT_INTERVAL) > attempt
253+
else timeout
254+
)
255+
elapsed_time = time.time() - attempt_start
256+
if (current_timeout * 0.001 - elapsed_time) > 0:
257+
await asyncio.sleep(current_timeout * 0.001 - elapsed_time)
258+
259+
attempt_start = time.time()
260+
244261
is_last_attempt = attempt == max_retries - 1
245262
force_not_use_cache = is_last_attempt
246263

@@ -282,7 +299,4 @@ async def _get_element(
282299
f"Attempt {attempt + 1}: Failed to get elements from selector, trying again "
283300
)
284301

285-
if not is_last_attempt:
286-
await asyncio.sleep(timeout * 0.001)
287-
288302
return None

0 commit comments

Comments
 (0)