kaliiiiiiiiii / Selenium-Driverless

undetected Selenium without usage of chromedriver
https://kaliiiiiiiiii.github.io/Selenium-Driverless/
Other
412 stars 52 forks source link

[BUG] different iframes share same window #241

Closed Vinyzu closed 3 weeks ago

Vinyzu commented 3 weeks ago

Reproducable Code Snippet (Edited from Playwright Tests):

from selenium_driverless import webdriver
from selenium_driverless.types.by import By
import asyncio

EMPTY_PAGE = "https://google.com/blank.html"

async def attach_frame(driver: webdriver.Chrome, frame_id: str, url: str):
    script = f"window.{frame_id} = document.createElement('iframe'); window.{frame_id}.src = '{url}'; window.{frame_id}.id = '{frame_id}'; document.body.appendChild(window.{frame_id});await new Promise(x => window.{frame_id}.onload = x);"
    await driver.eval_async(script)
    frame = await driver.execute_script(f"return window.{frame_id}")
    return frame

async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        await driver.get(EMPTY_PAGE)
        await attach_frame(driver, "frame0", EMPTY_PAGE)
        await attach_frame(driver, "frame1", EMPTY_PAGE)

        iframes = await driver.find_elements(By.TAG_NAME, "iframe")

        assert len(iframes) == 2
        [frame1, frame2] = iframes

        await asyncio.gather(
            frame1.execute_script("window.a = 1"), frame2.execute_script("window.a = 2")
        )
        [a1, a2] = await asyncio.gather(
            frame1.execute_script("return window.a"), frame2.execute_script("return window.a")
        )
        print(a1, a2) # 2 2 (Should be: 1 2)
        assert a1 == 1
        assert a2 == 2

asyncio.run(main())
kaliiiiiiiiii commented 3 weeks ago

wrong syntax, the test should be using iframe.content_document. See documentation