When page not loaded return empty state - fixes break for not loading page

This commit is contained in:
magmueller
2024-11-23 15:39:47 +01:00
parent 1112fbfd8c
commit 60fbd16730
4 changed files with 13 additions and 8 deletions

4
.gitignore vendored
View File

@@ -164,4 +164,6 @@ temp
tmp
.DS_Store
.DS_Store
private_example.py

View File

@@ -117,9 +117,9 @@ class Agent:
async def step(self) -> None:
"""Execute one step of the task"""
logger.info(f'\n📍 Step {self.n_steps}')
state = await self.controller.browser.get_state(use_vision=self.use_vision)
state = None
try:
state = await self.controller.browser.get_state(use_vision=self.use_vision)
model_output = await self.get_next_action(state)
result = await self.controller.act(model_output.action)
if result.extracted_content:
@@ -129,7 +129,7 @@ class Agent:
self.consecutive_failures = 0
except Exception as e:
result = self._handle_step_error(e, state)
result = self._handle_step_error(e)
model_output = None
if result.error:
@@ -139,11 +139,11 @@ class Agent:
error=result.error,
)
)
if state:
self._update_messages_with_result(result)
self._make_history_item(model_output, state, result)
self._update_messages_with_result(result)
self._make_history_item(model_output, state, result)
def _handle_step_error(self, error: Exception, state: BrowserState) -> ActionResult:
def _handle_step_error(self, error: Exception) -> ActionResult:
"""Handle all types of errors that can occur during a step"""
error_msg = AgentError.format_error(error)
prefix = f'❌ Result failed {self.consecutive_failures + 1}/{self.max_failures} times:\n '

View File

@@ -19,6 +19,8 @@ async def test_has_title(page: Page):
# Get all DOM content including all shadow roots recursively
start_time = time.time()
# wait for the page to load
await page.wait_for_load_state('load')
full_content = await dom_service._get_html_content()
# full_content = page.evaluate("""() => {
# function getAllContent(root) {

View File

@@ -36,6 +36,7 @@ class DomService:
if with_shadow_roots:
full_content = await self.page.evaluate("""() => {
function getAllContent(root) {
if (!root) return '';
let content = root.innerHTML || '';
// Get all elements with shadow roots