0

I'm trying to learn about 2Captcha nad as an example i wanted to make a spotify account. I've gotten to fill the form just right, but the only concern is the 2Captcha. I've tried all sorts of ways found on here but none of them worked. I've tried:

  • ___grecaptcha_cfg.clients[0].bL.K.callback('token');
  • window.captchaSuccessCallback(); (this was found in grecaptcha.render method)
  • And this was my last approach:
    val captcha = driver.findElement(By.id("captcha-div"))
    val siteKey = captcha?.getAttribute("data-sitekey") ?: ""
    println("Site key: $siteKey")

    val solvedCaptcha = getCaptcha(siteKey, "2captchaKey", driver.currentUrl)
    val js = driver as JavascriptExecutor
    println(solvedCaptcha)
    js.executeScript("document.getElementById('g-recaptcha-response').innerHTML='$solvedCaptcha';")
    Thread.sleep(500)

    val iframe = driver.findElement(By.xpath("//iframe[@title='recaptcha challenge']"))
    println(iframe.toString())
    driver.switchTo().frame(iframe)
    js.executeScript("document.getElementById('recaptcha-verify-button').click();")

the url i'm using is Here

Updated code (still not working) added simulating keystrokes in hopes that maybe the callback would get triggered after detecting any key presses:

val captcha = driver.findElement(By.id("captcha-div"))
    val siteKey = captcha?.getAttribute("data-sitekey") ?: ""
    println("Site key: $siteKey")

    val js = driver as JavascriptExecutor

    val findElement = driver.findElement(By.id("g-recaptcha-response"))
    js.executeScript("document.getElementById(\"g-recaptcha-response\").style.display = \"inline\";")
    val solvedCaptcha = getCaptcha(siteKey, "captchaKey", driver.currentUrl)
    println(solvedCaptcha)
    solvedCaptcha?.forEach {
        findElement.sendKeys(it.toString())
        Thread.sleep(Random.nextLong(5L, 30L))
    }
    Thread.sleep(10000)
qverkk
  • 13
  • 7
  • _...concern is the 2Captcha..._, Do you want to click the reCAPTCHA? – undetected Selenium Nov 29 '19 at 20:41
  • @DebanjanB i want to force a callback somehow, as there is no form to submit it, so the reCAPTCHA is never validated correctly even if i pass through the token into recaptcha-response – qverkk Nov 29 '19 at 20:51
  • So where are you exactly stuck? Where do you need help? – undetected Selenium Nov 29 '19 at 20:56
  • @DebanjanB I'm trying to execute something like this: https://2captcha.com/2captcha-api#callback , but i'm having issues detecting the callback or the submit form in order to perform the action of validating the reCAPTCHA, it stays still and if i click the register button it displays an error "Please verify that you're not a robot" – qverkk Nov 29 '19 at 21:02
  • At this point I would avoid a comment on how to execute something like`https://2captcha.com/2captcha-api#callback` but I can help you to click the reCAPTCHA. Let me know if that would suffice your requirement. – undetected Selenium Nov 29 '19 at 21:06

2 Answers2

0

To click() on the check box associated with the , as the the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following Locator Strategies:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_experimental_option("excludeSwitches", ["enable-automation"])
      options.add_experimental_option('useAutomationExtension', False)
      driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("https://www.spotify.com/gr/signup/")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
      
  • Browser Snapshot:

reCAPTCHA

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    This is helpful but not what i was looking for. I'm trying to get captcha solved so i can proceed throught the registration. However this iis helpfull with all those WebDriverWaits, before i've made my own conditional methods for detecting xpaths and Callables. Thank you ;) – qverkk Nov 29 '19 at 21:23
  • I already diid upvote ;) but it doesn't show untill i have 15 reputation sorry for that – qverkk Nov 29 '19 at 21:28
  • This doesn't work, you should not be interecting with that iframe. – pguardiario Nov 30 '19 at 03:06
0

This looks wrong:

js.executeScript("document.getElementById('g-recaptcha-response').innerHTML='$solvedCaptcha';")

I'm not sure how string interpolation works in java but I don't think it works like this.

Make sure that value is getting set and then click the green button and that should work.

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • I've even added simulating key strokes but it still doesn't work after i press the green button ``` solvedCaptcha?.forEach { findElement.sendKeys(it.toString()) Thread.sleep(Random.nextLong(5L, 30L)) } Thread.sleep(10000) ``` any tips on this? – qverkk Nov 30 '19 at 08:10
  • My tip is to update your question code and also to set a breakpoint to inspect that value before the submit. – pguardiario Nov 30 '19 at 11:19
  • @pguardiiario also, i'm writing this in kotlin, but the solution for the captcha should be the same as it runs on a JVM. I've already got to the point where i get the token from a captcha service, but i have no clue how to fill it on the website so captcha gets approved. – qverkk Nov 30 '19 at 13:32
  • I can't help with Koplin, sorry. I honestly have no idea wtf that is. – pguardiario Dec 01 '19 at 10:45
  • You could help with a java snippet and i could just take tips from it if you're keen on helping like that ;) – qverkk Dec 01 '19 at 15:18
  • What I'm saying is that I believe you are literally setting that value to "$solvedCaptcha" so just make sure that's not happening. – pguardiario Dec 02 '19 at 00:56