franklinCSS-demo / flasktest /cloneNonDefault.py
PrakharPratap's picture
initial commit
1f8f99f
import asyncio
import json
from pyppeteer import launch
async def compare_styles(url, container_selector):
browser = await launch()
page = await browser.newPage()
await page.goto(url)
container_element = await page.querySelector(container_selector)
if container_element:
await compare_element_styles(page, container_element, container_selector)
else:
print(f'Container element with selector "{container_selector}" not found.')
await browser.close()
async def compare_element_styles(page, element, identifier):
clone_selector = '__cloned_element__'
await page.evaluate(f'el => el.cloneNode(true)', element, force_expr=True)
await page.evaluate(f'el => {{ el.style.all = "unset"; el.setAttribute("id", "{clone_selector}"); }}', element, force_expr=True)
clone_handle = await page.querySelector(f'#{clone_selector}')
computed_styles = await element.executionContext().evaluate('(el) => JSON.stringify(getComputedStyle(el))', element)
clone_styles = await clone_handle.executionContext().evaluate('(el) => JSON.stringify(getComputedStyle(el))', clone_handle)
print(f'Here are the styles for element {identifier}:')
computed_styles_dict = json.loads(computed_styles)
clone_styles_dict = json.loads(clone_styles)
for style_property, actual_value in computed_styles_dict.items():
clone_value = clone_styles_dict.get(style_property)
if actual_value != clone_value:
print(f'{style_property}: {actual_value}')
child_elements = await page.querySelectorAll(f'{element._remoteObject.description} > *')
for i, child_element in enumerate(child_elements):
await compare_element_styles(page, child_element, f'{identifier}.{i}')
url = input('Enter the URL of the webpage: ')
container_selector = input('Enter the container selector: ')
asyncio.get_event_loop().run_until_complete(compare_styles(url, container_selector))