kaliiiiiiiiii / Selenium-Driverless

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

missing some iframes #68

Closed Fragaile closed 11 months ago

Fragaile commented 11 months ago

I tried everything, read every closed issue and all your documentation, iframes will just refuse to be found.

Modified code (CLASS_NAME is js-frame, because that's what my site uses)

iframes = driver.find_elements(By.CLASS_NAME, "js-frame")
sleep(5)
driver.get_target_for_iframe(iframes[0])

Element is found but driver.get_target_for_iframe() doesnt work, it throws this error:

    raise NoSuchIframe(iframe, "no target for iframe found")
selenium_driverless.types.target.NoSuchIframe: no target for iframe found

If I do the same with selenium, it works perfectly.

iframes = driver.find_elements(By.CLASS_NAME, "js-iframe")
sleep(5)
driver.switch_to.frame(iframes[0])
kaliiiiiiiiii commented 11 months ago

are you sure the found elements are iframes? Also, please provide the relevant HTML

Fragaile commented 11 months ago

Thanks for replying,

I'm not looking for elements, I just want to switch to the iframe, like I said, it works in selenium. I don't know what kind of html you want me to provide but here's the iframe I'm trying to switch to.

HTML:

<span data-cse="encryptedSecurityCode" data-uid="adyen-checkout-encryptedSecurityCode-XXXX" class="adyen-checkout__input adyen-checkout__input--small adyen-checkout__card__cvc__input CardInput-module_adyen-checkout__input__11tlB">
   <iframe src="https://checkoutshopper-live.adyen.com/checkoutshopper/securedfields/live_XXXXXXXXXXXXXX/4.5.0/securedFields.html?type=card&amp;d=XXXXXXXXXXXXXXX=" class="js-iframe" title="Iframe for security code" allowtransparency="true" style="border: none; height:100%; width:100%; overflow:hidden;" referrerpolicy="origin">
      <p>Your browser does not support iframes.</p>
   </iframe>
</span>
kaliiiiiiiiii commented 11 months ago

@Fragaile can you provide what await iframes[0].source gives you? Also, as long you f don't provide the full relevant HTML, I can't reproduce this.

Fragaile commented 11 months ago

@kaliiiiiiiiii print(iframes[0].source) gives me:

<iframe src="https://checkoutshopper-live.adyen.com/checkoutshopper/securedfields/live_XXXXXXXX/4.5.0/securedFields.html?type=card&amp;d=XXXXXXXX=" class="js-iframe" title="Iframe for security code" allowtransparency="true" style="border: none; height:100%; width:100%; overflow:hidden;" referrerpolicy="origin">
   <p>Your browser does not support iframes.</p>
</iframe>

The 'XXXXX' areas are just blanked out by me, not relevant info.

Do you want me to provide the entire page source html? I'm pretty sure it would be broken, but I'll try.

https://pastebin.com/MkJDBAXB

kaliiiiiiiiii commented 11 months ago

@Fragaile Huh thanks, this actually seems to be a bug. I'll do some testing. Thanks for opening this issue btw!

Fragaile commented 11 months ago

@kaliiiiiiiiii No problem, thank you too 💯

kaliiiiiiiiii commented 11 months ago

@Fragaile

  1. Could you try if this applies to the devversion as well? ( install with pip install https://github.com/kaliiiiiiiiii/Selenium-Driverless/archive/refs/heads/dev.zip)
  2. Could you check if the following test script (uses iframes) runs correctly in your env? (from /tests/bypass_turnstile.py)
    
    import asyncio

from selenium_driverless import webdriver from selenium_driverless.types.by import By from selenium_driverless.types.webelement import NoSuchElementException

async def main(): options = webdriver.ChromeOptions()

options.add_argument("--headless=new")

async with webdriver.Chrome(options=options) as driver:
    await driver.get("https://nopecha.com/demo/turnstile")
    await asyncio.sleep(0.5)

    # some random mouse-movements over iframes
    pointer = await driver.current_pointer
    await pointer.move_to(500, 200, smooth_soft=60, total_time=0.5)
    await pointer.move_to(20, 50, smooth_soft=60, total_time=0.5)
    await pointer.move_to(8, 45, smooth_soft=60, total_time=0.5)
    await pointer.move_to(500, 200, smooth_soft=60, total_time=0.5)
    await pointer.move_to(166, 206, smooth_soft=60, total_time=0.5)
    await pointer.move_to(200, 205, smooth_soft=60, total_time=0.5)

    iframes = await driver.find_elements(By.TAG_NAME, "iframe")
    await asyncio.sleep(0.5)
    targets = await driver.get_targets_for_iframes(iframes)

    target = None
    for target in targets:
        # filter out correct iframe target
        text = None
        try:
            elem = await target.find_element(By.CSS_SELECTOR, "body > div.overlay")
            text = await elem.text
        except NoSuchElementException:
            pass
        finally:
            if text:  # 'Only a test.' text
                break
    if not target:
        raise Exception("correct target for iframe not found")

    src = await driver.page_source
    checkbox = await target.find_element(By.CSS_SELECTOR, "#challenge-stage > div > label > input[type=checkbox]", timeout=20)
    await checkbox.click(move_to=True)
    await asyncio.sleep(5)
    print("saving screenshot")
    await driver.save_screenshot("turnstile_captcha.png")

asyncio.run(main())



I can't reproduce this issue :(
kaliiiiiiiiii commented 11 months ago

related to https://github.com/ChromeDevTools/devtools-protocol/issues/72, not that easy to resolve.

Fragaile commented 11 months ago

@kaliiiiiiiiii Sorry for the wait, your demo returns this screenshot: turnstile_captcha

And "correct target for iframe not found" isn't printed, so it works.

For some weird reason, this site that I'm trying to automate doesn't work with your code.. Have you figured out a solution yet?

kaliiiiiiiiii commented 11 months ago

@Fragaile Yep, the solution is already available in the dev-branch and will be released probably within about 24hrs. (see Readme.md in dev-branch for updaten usage and installation)

Basically, it occurs because some iframes are now targets (some cross-origin ones) and others are within the same target.

Fragaile commented 11 months ago

@kaliiiiiiiiii iframe_document = await iframes[0].content_document

.content_document doesnt seem to be an actual thing.. I tried reinstalling your package.

AttributeError: 'WebElement' object has no attribute 'content_document'

NEVERMIND!! I installed the regular version, not dev, sorry.

kaliiiiiiiiii commented 11 months ago

@Fragaile This should be resolved with selenium-driverless==1.6 Please close this issue if it's fixed now:)