使用Selenium通过Recaptcha验证 如何触发无图片校验机制
reCAPTCHA无图片校验触发要求
- 浏览器环境无自动化特征:reCAPTCHA会检测
navigator.webdriver属性、浏览器启动参数、插件列表、用户代理等特征,默认配置的Selenium驱动存在大量明确的自动化标记,会被直接判定为非人类操作,触发图片校验 - 操作行为符合真人特征:真人点击复选框前会有随机的鼠标移动轨迹、点击时延、停留时间,你代码中直接通过JS执行点击的操作没有对应的鼠标交互事件,特征高度可疑,几乎都会触发强校验
- IP与账号历史正常:当前使用的IP没有频繁的验证请求记录,浏览器有正常的浏览历史与cookie数据,无批量操作痕迹
可行实现方案
- 第一步:修改Selenium启动配置,抹去自动化特征
启动ChromeDriver时添加如下配置,消除默认的识别标记:ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-blink-features=AutomationControlled"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", false); // 可选:加载已有用户数据目录,复用正常浏览器的cookie与使用记录 options.addArguments("user-data-dir=C:/Users/你的用户名/AppData/Local/Google/Chrome/User Data"); - 第二步:替换JS点击为拟人操作
移除固定10秒的sleep与JS点击逻辑,改用Actions类模拟真人鼠标操作:// 替换原有点击逻辑 Random random = new Random(); Thread.sleep(1000 + random.nextInt(2000)); WebElement captchaBox = driver.findElement(By.cssSelector("div.recaptcha-checkbox-checkmark")); Actions actions = new Actions(driver); // 模拟鼠标移动到元素上方,随机偏移 actions.moveToElement(captchaBox, random.nextInt(10), random.nextInt(10)) .pause(random.nextInt(500) + 300) .click() .build() .perform(); - 第三步:极端情况兼容
如果调整后仍偶尔触发图片校验,可以接入商用的验证码识别服务,通过接口提交验证码图片获取识别结果,全程无需人工介入。
你提交的原始代码(已格式化)
SoftAssert Sa = new SoftAssert(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(20000)); JavascriptExecutor js = (JavascriptExecutor) driver; String FormatMessageInformatife = "<font color='#73a9d0'></br><i class='fas fa-info-circle'></i>- "; String FormatMessageError = "<font color='#bf3d27'></br><i class='fas fa-times-circle'></i>- "; String FormatMessageSucces = "<font color='#69c158'></br><i class='fas fa-check-circle'></i>-"; WebElement ClkSignup = driver.findElement(By.id("toggle-button")); ClkSignup.click(); WebElement ClkSubscribe = driver .findElement(By.xpath("//div[@id=\"dialog-07zr\"]/div[@class=\"boutons-yellow\"]/a[1]/button")); ClkSubscribe.click(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='steps-title']/ul/li[@class='active'][1]/span[normalize-space(text())='Coordonnées']"))); Boolean AccesSubscription = driver.findElements(By.xpath("//div[@class='steps-title']/ul/li[@class='active'][1]/span[normalize-space(text())='Coordonnées']")).size() > 0; if(AccesSubscription == true) { Reporter.log(FormatMessageSucces + " Accessed Subscription </font>"); WebElement TVAInput = driver.findElement(By.id("create_account_step1_tva")); sendHumanKeys(TVAInput,"FR75850236541"); WebElement EmailInput = driver.findElement(By.id("create_account_step1_mail")); sendHumanKeys(EmailInput,"test2021test@gmail.com"); WebElement PhoneInput = driver.findElement(By.id("create_account_step1_phone")); sendHumanKeys(PhoneInput,"0721547861"); WebElement PwdInput = driver.findElement(By.id("create_account_step1_password_first")); sendHumanKeys(PwdInput,"Test07ZR"); WebElement CPwdInput = driver.findElement(By.id("create_account_step1_password_second")); sendHumanKeys(CPwdInput,"Test07ZR"); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); Thread.sleep(10000); WebElement CaptchaClk = driver.findElement(By.cssSelector("div.recaptcha-checkbox-checkmark")); js.executeScript("arguments[0].click();", CaptchaClk); WebElement RBClk = driver.findElement(By.xpath("//div[@id=\"checkbox\"]/label")); js.executeScript("arguments[0].click();", RBClk); WebElement ClkNext = driver.findElement(By.id("bt")); js.executeScript("arguments[0].click();", ClkNext); }else { Sa.assertEquals(false, true, "Wasn't abel to acces Subscription"); ScreenShot(driver); Reporter.log(FormatMessageError + " Wasn't abel to acces Subscription</font>"); }
内容的提问来源于stack exchange,提问作者Yan
相关产品推荐
相关产品推荐

