Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Linter

Magnus Müllerb5afe2fece12
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 45dc1c32a..56bd53e82 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1059,22 +1059,22 @@ Debug: Original and validated code {'differ' if code != validated_code else 'are
def _validate_and_fix_javascript(self, code: str) -> str:
"""Validate and fix common JavaScript issues before execution"""
-
+
import re
-
+
# Pattern 1: Fix double-escaped quotes (\\\" → \")
# This happens when quotes get double-escaped in the parsing process
- fixed_code = re.sub(r'\\\\"', '"', code)
-
+ fixed_code = re.sub(r'\\"', '"', code)
+
# Pattern 2: Fix XPath expressions with mixed quotes
xpath_pattern = r'document\.evaluate\s*\(\s*"([^"]*\'[^"]*)"'
def fix_xpath_quotes(match):
xpath_with_quotes = match.group(1)
return f'document.evaluate(`{xpath_with_quotes}`'
-
+
fixed_code = re.sub(xpath_pattern, fix_xpath_quotes, fixed_code)
-
+
# Pattern 3: Fix querySelector/querySelectorAll with mixed quotes
selector_pattern = r'(querySelector(?:All)?)\s*\(\s*"([^"]*\'[^"]*)"'
@@ -1082,52 +1082,52 @@ Debug: Original and validated code {'differ' if code != validated_code else 'are
method_name = match.group(1)
selector_with_quotes = match.group(2)
return f'{method_name}(`{selector_with_quotes}`'
-
+
fixed_code = re.sub(selector_pattern, fix_selector_quotes, fixed_code)
-
+
# Pattern 4: Fix closest() calls with mixed quotes
closest_pattern = r'\.closest\s*\(\s*"([^"]*\'[^"]*)"'
def fix_closest_quotes(match):
selector_with_quotes = match.group(1)
return f'.closest(`{selector_with_quotes}`'
-
+
fixed_code = re.sub(closest_pattern, fix_closest_quotes, fixed_code)
-
+
# Pattern 5: Fix getAttribute calls with mixed quotes
get_attr_pattern = r'\.getAttribute\s*\(\s*"([^"]*\'[^"]*)"'
def fix_get_attr_quotes(match):
attr_with_quotes = match.group(1)
return f'.getAttribute(`{attr_with_quotes}`'
-
+
fixed_code = re.sub(get_attr_pattern, fix_get_attr_quotes, fixed_code)
-
+
# Pattern 6: Fix CSS :has() selector (not supported in all browsers)
# Replace :has() with more compatible selectors where possible
has_pattern = r'(["\']?)([^"\']*):has\([^)]+\)([^"\']*)\1'
-
+
def fix_has_selector(match):
quote = match.group(1)
prefix = match.group(2)
suffix = match.group(3)
# Remove :has() part and just use the base selector
return f'{quote}{prefix}{suffix}{quote}'
-
+
fixed_code = re.sub(has_pattern, fix_has_selector, fixed_code)
-
+
# Log changes made
changes_made = []
- if '\\\"' in code and '\\\"' not in fixed_code:
+ if r'\"' in code and r'\"' not in fixed_code:
changes_made.append('fixed double-escaped quotes')
if '`' in fixed_code and '`' not in code:
changes_made.append('converted mixed quotes to template literals')
if ':has(' in code and ':has(' not in fixed_code:
changes_made.append('removed unsupported :has() selector')
-
+
if changes_made:
logger.debug(f'JavaScript fixes applied: {", ".join(changes_made)}')
-
+
return fixed_code
def _register_done_action(self, output_model: type[T] | None, display_files_in_done_text: bool = True):