Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Codestyle fixes
Evgeny Kim2cba10dafc43
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex ab995641d..c1e34c4c0 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1932,10 +1932,10 @@ class BrowserSession(BaseModel):- Check if it's a label pointing to a file input- Recursively search children for file inputs- Check siblings for file inputs-+Args:index: The index of the candidate element (could be a file input, label, or parent element)-+Returns:The DOM element for the file input if found, None otherwise"""@@ -1943,12 +1943,12 @@ class BrowserSession(BaseModel):selector_map = await self.get_selector_map()if index not in selector_map:return None-+candidate_element = selector_map[index]-+def is_file_input(node: DOMElementNode) -> bool:return isinstance(node, DOMElementNode) and node.tag_name == 'input' and node.attributes.get('type') == 'file'-+def find_element_by_id(node: DOMElementNode, element_id: str) -> DOMElementNode | None:if isinstance(node, DOMElementNode):if node.attributes.get('id') == element_id:@@ -1958,22 +1958,24 @@ class BrowserSession(BaseModel):if result:return resultreturn None-+def get_root(node: DOMElementNode) -> DOMElementNode:root = nodewhile root.parent:root = root.parentreturn root-+# Recursively search for file input in node and its children- def find_file_input_recursive(node: DOMElementNode, max_depth: int = 3, current_depth: int = 0) -> DOMElementNode | None:+ def find_file_input_recursive(+ node: DOMElementNode, max_depth: int = 3, current_depth: int = 0+ ) -> DOMElementNode | None:if current_depth > max_depth or not isinstance(node, DOMElementNode):return None-+# Check current elementif is_file_input(node):return node-+# Recursively check childrenif node.children and current_depth < max_depth:for child in node.children:@@ -1982,25 +1984,25 @@ class BrowserSession(BaseModel):if result:return resultreturn None-+# Check if current element is a file inputif is_file_input(candidate_element):return candidate_element-+# Check if it's a label pointing to a file inputif candidate_element.tag_name == 'label' and candidate_element.attributes.get('for'):input_id = candidate_element.attributes.get('for')root_element = get_root(candidate_element)-+target_input = find_element_by_id(root_element, input_id)if target_input and is_file_input(target_input):return target_input-+# Recursively check childrenchild_result = find_file_input_recursive(candidate_element)if child_result:return child_result-+# Check siblingsif candidate_element.parent:for sibling in candidate_element.parent.children:@@ -2008,9 +2010,9 @@ class BrowserSession(BaseModel):if is_file_input(sibling):return siblingreturn None-+except Exception as e:- logger.debug(f"Error in find_file_upload_element_by_index: {e}")+ logger.debug(f'Error in find_file_upload_element_by_index: {e}')return None@require_initialization