pythonselenium-webdriveriframecaptcha

How do I solve an Arkose FunCaptcha on LinkedIn using 2Captcha when the CAPTCHA is embedded within an iframe?


I am attempting to automate solving CAPTCHAs when logging into LinkedIn via 2Captcha in my Python/Selenium/Google Chrome script.

To do so, per the answer of a previous question I asked, I load the CRX file for the 2Captcha Chrome Extension when instantiating my webdriver, and enabling autosolve for all CAPTCHAs.

However, for LinkedIn, the extension is unable to detect the CAPTCHA because it is inside an <iframe></iframe> element of the webpage. To bypass, this, I added the following to my Python script to detect and solve the CAPTCHA:

This script below still does not detect and properly solve FunCaptchas. May I know what I can do to detect and solve LinkedIn CAPTCHAs that are inside an <iframe></iframe>? For reference, an image of what this CAPTCHA and a snippet of LinkedIn's code (below mine) are attached.

My Python Code:

try:
    solver = TwoCaptcha('<API_KEY>')
    captcha = driver.find_element(By.XPATH, 
                                    "//input[@name='captchaSiteKey']")
    captcha_sitekey = captcha.get_attribute('value')
    iframe = driver.find_element(By.XPATH, "//iframe")
    iframe_source = iframe.get_attribute('src')
    driver.switch_to.frame(iframe)
    
    print(f'IFRAME Source: {iframe_source}')
    print(f'CAPTCHA SiteKey: {captcha_sitekey}')

    time.sleep(60)
    result = solver.funcaptcha(sitekey=captcha_sitekey, 
                                url=iframe_source)
    
    for key in result:
        print(f'{key}: {result[key]}')
    
    print('We found and solved a CAPTCHA.')
    driver.switch_to.default_content()
except Exception as e:
    print(f'We could not solve the CAPTCHA because {str(e)}, '
          f'{traceback.print_exc}')

(I did not write it in the block of code above, but I'm importing the traceback, 2captcha-python, and selenium modules.

LinkedIn's HTML on the CAPTCHA page:

<iframe class="" id="captcha-internal" src="./Security Verification _ LinkedIn_files/funCaptchaInternal.html"
    frameborder="0" role="presentation" title="Captcha Challenge" height="290" width="302"></iframe>
<form method="post" class="hidden" id="captcha-challenge" action="https://www.linkedin.com/checkpoint/challenge/verify">
    <input type="hidden" name="csrfToken" value="ajax:6505936439590984220"><input type="hidden" name="captchaSiteKey"
        value="3117BF26-4762-4F5A-8ED9-A85E69209A46"><input type="hidden" name="challengeId"
        value="AQFvUtZMWireaAAAAYvTP-Lq8KvlyrCr7xCgZlth6uf4UoJSq7UrP1-4bNpriHtPql8mOpZipQF3qa1vNAec2TT9aef2xMVB0Q"><input
        type="hidden" name="language" value="en-US"><input type="hidden" name="displayTime"
        value="AgFP_RD3wseHwQAAAYvTP-L3CpYje8Fh1rkGPsUfvp7WGYv0NvYEqR_gAaCDCew"><input type="hidden"
        name="challengeType" value="AgFU-zncyoLVdAAAAYvTP-L66dj0drF5826EpedJld3sNEQ0b8diSPFWlBU"><input type="hidden"
        name="challengeSource" value="AgGudd74ELMaKQAAAYvTP-L9K5-uGH1mPDpZy1yUGcdWEbCi3jtReJS_a-ufgicE"><input
        type="hidden" name="requestSubmissionId"
        value="AgF0RnGQ2BTi9QAAAYvTP-MA56TcJxjPSm3P71dof2-LrpBUMFM96V4_2m0pYDnLWnYMTPz9HIZ-TuCX35YFJgKBYom7WXc3eTGfrQ"><input
        type="hidden" name="captchaUserResponseToken" value=""><input type="hidden" name="challengeData"
        value="AgFlYfn-cheGuwAAAYvTP-LySUtucNcd5hW7d3PzwesMuCW97DRwpH_XItP5Ynt4E_dL4mZC18kF-5UGtjZ_l25oIZXIrhpW_qjH84--g65ZGktWQJloVkNrMiY3BrOTB1PuqhvwDnYhJj3o-jM54s5Ap1-0U7zDiU6lgAkA92Ez01S2-PBr15R44ZrVoFQPuRhOsUKQdSjREh4sPw6zjyx-9EYXEgPwnwWbCNjT8GpYr1GkobR0bQsa0RLjgkSOQ7KxV4iTVoYYHgt7UFssZQykI0PdVypaYVft3OoZTewLoz2Tg6GcIU58TItd4ulUP8gGj4NpKsQiMtqxf9mxs1hxkKtoOt29ZJ3RPa0Ge9z0bUYzjKChJNdORYHzSJcmnBPY4az1z3mw4hh7F0wbzZouuAb6yCwGtX_6sEnuaewCHBSaEbv-VwmNgDGk8-k_z-seh_2z2Q-s5gPanbEPBjhhseq9OBweFlXmSRf6h9KTlcizSGgBx7Yz9YBirkob2ukkxuU9yhmZ9911cUVMJ-ZaQU0jNOZW9PEX4ZLinEAWi5ei3X2bXfIc3lVhQ89RN-6t9G9uRFbOKOcrisiPt2MgDTBVni0QLkPcDOQyAahyxiTwJg8ksp3cPi16xZd4zr2VHiIT0Dm8CuI4GPImNdfMKjqZWuPjS7KC0_CtHvXa03cwfQAXSYGzDIX7obdutQ2XWT3JyXF4sz3o03bPIn2p0f7Aejd2yI6AH5krsacMqaVrSfLjSBq4xRBjLLg"><input
        type="hidden" name="pageInstance"
        value="urn:li:page:d_checkpoint_ch_captchaV2Challenge;KXxlvP5+SpygwcfBd+7rDg=="><input type="hidden"
        name="challengeDetails"
        value="AgFwKVDCcGn56wAAAYvTP-L1wL8sH9K8QYfqk4ydRmaR9VTdhiElPBT1pJ9zSFYaYZU7F6ulGXRJjDXGNCiOU_57wWr4ae19I_rc9qVKYlm5ggO43Yr8Q_AJX6TOv5s_fqbcAEakFbLgVvIu46NVK_TI1lwtIXay"><input
        type="hidden" name="failureRedirectUri"
        value="AgFg3MLPYMreRAAAAYvTP-MC_VIvK3O8g0pYH_FBHDR5q1XBY77au_DHDHrV1c78mXMKKhjrXIC-8vKyXQZuC7ashO7hSP0qRmwORfm-ECf1Tjuisx6n_Q"><input
        type="hidden" name="signInLink" value="/checkpoint/lg/login?trk=homepage-basic_sign-in-submit"><input
        type="hidden" name="joinNowLink" value="/signup/cold-join?source=homepage-basic_sign-in-submit"><input
        type="hidden" name="_s" value="CONSUMER_LOGIN">
</form>

LinkedIn CAPTCHA image.


Solution

  • By just looking at your code I think you need to switch to the iframe before you search for the captcha like this:

    iframe = driver.find_element(By.XPATH, "//iframe")
    iframe_source = iframe.get_attribute('src')
    driver.switch_to.frame(iframe) 
    captcha = driver.find_element(By.XPATH, 
                                    "//input[@name='captchaSiteKey']")
    captcha_sitekey = captcha.get_attribute('value')
    

    Because without switching to the iframe, selenium can't find elements inside it.

    If that doesn't help: Where does your script fail? What is the output of the print statements?