Skip to content

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

Enhance debug_page_structure with advanced element and viewport analysis

Magnus Müllerde6618ab6359
diff --git a/browser_use/dom/tests/debug_page_structure.py b/browser_use/dom/tests/debug_page_structure.py
index f55df33c7..49e99bbc2 100644
--- a/browser_use/dom/tests/debug_page_structure.py
+++ b/browser_use/dom/tests/debug_page_structure.py
@@ -9,7 +9,7 @@ from browser_use.browser.context import BrowserContext
async def analyze_page_structure(url: str):
- """Analyze and print the structure of a webpage"""
+ """Analyze and print the structure of a webpage with enhanced debugging"""
browser = Browser(
config=BrowserConfig(
headless=False, # Set to True if you don't need to see the browser
@@ -25,9 +25,83 @@ async def analyze_page_structure(url: str):
await page.goto(url)
await page.wait_for_load_state('networkidle')
- # Get and print the page structure
- structure = await ctx.get_page_structure()
+ # Get viewport dimensions
+ viewport_info = await page.evaluate("""() => {
+ return {
+ viewport: {
+ width: window.innerWidth,
+ height: window.innerHeight,
+ scrollX: window.scrollX,
+ scrollY: window.scrollY
+ }
+ }
+ }""")
+
+ print('\nViewport Information:')
+ print(f'Width: {viewport_info["viewport"]["width"]}')
+ print(f'Height: {viewport_info["viewport"]["height"]}')
+ print(f'ScrollX: {viewport_info["viewport"]["scrollX"]}')
+ print(f'ScrollY: {viewport_info["viewport"]["scrollY"]}')
+
+ # Enhanced debug information for cookie consent and fixed position elements
+ debug_info = await page.evaluate("""() => {
+ function getElementInfo(element) {
+ const rect = element.getBoundingClientRect();
+ const style = window.getComputedStyle(element);
+ return {
+ tag: element.tagName.toLowerCase(),
+ id: element.id,
+ className: element.className,
+ position: style.position,
+ rect: {
+ top: rect.top,
+ right: rect.right,
+ bottom: rect.bottom,
+ left: rect.left,
+ width: rect.width,
+ height: rect.height
+ },
+ isFixed: style.position === 'fixed',
+ isSticky: style.position === 'sticky',
+ zIndex: style.zIndex,
+ visibility: style.visibility,
+ display: style.display,
+ opacity: style.opacity
+ };
+ }
+
+ // Find cookie-related elements
+ const cookieElements = Array.from(document.querySelectorAll('[id*="cookie"], [id*="consent"], [class*="cookie"], [class*="consent"]'));
+ const fixedElements = Array.from(document.querySelectorAll('*')).filter(el => {
+ const style = window.getComputedStyle(el);
+ return style.position === 'fixed' || style.position === 'sticky';
+ });
+
+ return {
+ cookieElements: cookieElements.map(getElementInfo),
+ fixedElements: fixedElements.map(getElementInfo)
+ };
+ }""")
+
+ print('\nCookie-related Elements:')
+ for elem in debug_info['cookieElements']:
+ print(f'\nElement: {elem["tag"]}#{elem["id"]} .{elem["className"]}')
+ print(f'Position: {elem["position"]}')
+ print(f'Rect: {elem["rect"]}')
+ print(f'Z-Index: {elem["zIndex"]}')
+ print(f'Visibility: {elem["visibility"]}')
+ print(f'Display: {elem["display"]}')
+ print(f'Opacity: {elem["opacity"]}')
+
+ print('\nFixed/Sticky Position Elements:')
+ for elem in debug_info['fixedElements']:
+ print(f'\nElement: {elem["tag"]}#{elem["id"]} .{elem["className"]}')
+ print(f'Position: {elem["position"]}')
+ print(f'Rect: {elem["rect"]}')
+ print(f'Z-Index: {elem["zIndex"]}')
+
print(f'\nPage Structure for {url}:\n')
+ structure = await ctx.get_page_structure()
print(structure)
input('Press Enter to close the browser...')
@@ -37,5 +111,13 @@ async def analyze_page_structure(url: str):
if __name__ == '__main__':
# You can modify this URL to analyze different pages
- url = 'https://reddit.com'
- asyncio.run(analyze_page_structure(url))
+
+ urls = [
+ 'https://www.mlb.com/yankees/stats/',
+ 'https://immobilienscout24.de',
+ 'https://www.zeiss.com/career/en/job-search.html?page=1',
+ 'https://www.zeiss.com/career/en/job-search.html?page=1',
+ 'https://reddit.com',
+ ]
+ for url in urls:
+ asyncio.run(analyze_page_structure(url))
diff --git a/browser_use/dom/tests/extraction_test.py b/browser_use/dom/tests/extraction_test.py
index d48823f54..0a4dce638 100644
--- a/browser_use/dom/tests/extraction_test.py
+++ b/browser_use/dom/tests/extraction_test.py
@@ -88,16 +88,20 @@ async def test_focus_vs_all_elements():
browser = Browser(
config=BrowserConfig(
- chrome_instance_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
+ # chrome_instance_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
)
)
context = BrowserContext(browser=browser, config=config) # noqa: F821
websites = [
+ 'https://immobilienscout24.de',
+ 'https://www.zeiss.com/career/en/job-search.html?page=1',
+ 'https://www.mlb.com/yankees/stats/',
'https://www.amazon.com/s?k=laptop&s=review-rank&crid=1RZCEJ289EUSI&qid=1740202453&sprefix=laptop%2Caps%2C166&ref=sr_st_review-rank&ds=v1%3A4EnYKXVQA7DIE41qCvRZoNB4qN92Jlztd3BPsTFXmxU',
+ 'https://codepen.io/geheimschriftstift/pen/mPLvQz',
+ 'https://reddit.com',
'https://www.google.com/search?q=google+hi&oq=google+hi&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIGCAEQRRhA0gEIMjI2NmowajSoAgCwAgE&sourceid=chrome&ie=UTF-8',
'https://kayak.com/flights',
- 'https://immobilienscout24.de',
'https://google.com',
'https://amazon.com',
'https://github.com',