我试图以2Captcha nad为例,我想创建一个Spotify帐户。我已经正确地填写了表格,但唯一需要注意的是2Captcha。我尝试了这里找到的各种方法,但是没有一种起作用。我试过了:
___ grecaptcha_cfg.clients [0] .bL.K.callback('token');
window.captchaSuccessCallback(); (这在grecaptcha.render方法中找到)
这是我最后的方法:
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();")
我使用的网址是Here
更新的代码(仍然无法正常工作)添加了模拟击键,希望可能在检测到任何按键后触发回调:
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)
最佳答案
要在与recaptcha关联的复选框上选择click()
,因为所需的元素在<iframe>
内,因此您必须:
促使WebDriverWait提供所需的帧并切换到该帧。
促使WebDriverWait使所需的元素可单击。
您可以使用以下Locator Strategies:
代码块:
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()
浏览器快照: