【问题标题】:Java/Selenium webdriver: Unable to click the captcha iconJava/Selenium webdriver:无法单击验证码图标
【发布时间】:2018-08-04 21:39:12
【问题描述】:

我正在开发一个需要将显示的文本与显示的验证码图标匹配的门户。每个验证码图标都有一个 src 标签 URL。我已经使用模式从 URL 中提取了文本。但是,如果它与 if/else if 块中的文本匹配,则无法打印文本。

如果字符串与预期文本匹配,如何打印文本?

HTML:

<div id="captchaID_AN" captcha="" class="row custompanel captcha"><div class="small-12 columns margin-btm--small"><label class="captchalabel">Select the ( Computer ) </label><input id="captchaID_AN_hidden" name="captchaID_AN_hidden" type="hidden"><label for="captchaID_AN" data-error=""></label></div><div class="small-12 columns"><div class="small-2 columns captchaheight"><a class="captcha-items__item"><img id="5eb1f65fc77427a180aebfafa00f8d36" src="/obweb/javax.faces.resource/img/assets/computer.png.jsf?ln="></a></div><div class="small-2 columns captchaheight"><a class="captcha-items__item"><img id="d3d876396aea3e8da304747e4e4ffc9f" src="/obweb/javax.faces.resource/img/assets/music-note.png.jsf?ln="></a></div><div class="small-2 columns captchaheight"><a class="captcha-items__item"><img id="93bb735cad5164b841132845a33c9054" src="/obweb/javax.faces.resource/img/assets/balloons.png.jsf?ln="></a></div><div class="small-2 columns captchaheight"><a class="captcha-items__item"><img id="db9b4dc10f5eb9dbe350b68b3cd02126" src="/obweb/javax.faces.resource/img/assets/house.png.jsf?ln="></a></div><div class="small-2 columns captchaheight"><a class="captcha-items__item"><img id="dd9ba4c935057e29b55c99f3316ce004" src="/obweb/javax.faces.resource/img/assets/scissors.png.jsf?ln="></a></div><div class="small-2 columns"><a id="captchaID_AN_link" href="#" class=" icon-refresh captcharefresh " ng-click="click($event,{s:&quot;captchaID_AN&quot;,p:&quot;registrationenterdetails&quot;,u:&quot;captchaID_AN&quot;,iau:true,imm:true});" tabindex="0"><span class="icon refresh " style="margin-right:5px"></span>Reload</a></div></div></div>

代码:

WebElement text = driver.findElement(By.xpath("//label[contains(text(), 'Select the ')]"));
String texts = text.getText();

System.out.println("Text is:" + texts);

replacetext = texts.substring(texts.indexOf("(") + 1, texts.indexOf(")"));
System.out.println("replacedtextis:" + replacetext);

List < WebElement > list = driver.findElements(By.xpath("//*[@class='small-12 columns']/div/a/img"));

String firstcaptcha = list.get(0).getAttribute("src");
String secondcaptcha = list.get(1).getAttribute("src");
String thirdcaptcha = list.get(2).getAttribute("src");
String fourthcaptcha = list.get(3).getAttribute("src");
String fifthcaptcha = list.get(4).getAttribute("src");

Pattern p = Pattern.compile("assets/(\\w+.\\w+)");

Matcher m = p.matcher(firstcaptcha);
Matcher m1 = p.matcher(secondcaptcha);
Matcher m2 = p.matcher(thirdcaptcha);
Matcher m3 = p.matcher(fourthcaptcha);
Matcher m4 = p.matcher(fifthcaptcha);

if (m.find()) {
    System.out.println(m.group(1));

    int test = m.group(1).indexOf(".");

    if (test != -1) {
        subString = m.group(1).substring(0, test); //
        System.out.println("value is:" + subString);
    }


}

if (m1.find()) {
    System.out.println(m1.group(1));

    int test1 = m1.group(1).indexOf(".");

    if (test1 != -1) {
        substring1 = m1.group(1).substring(0, test1); //
        System.out.println("value is:" + substring1);
    }

}
if (m2.find()) {
    System.out.println(m2.group(1));

    int test2 = m2.group(1).indexOf(".");

    if (test2 != -1) {
        substring2 = m2.group(1).substring(0, test2); //
        System.out.println("value is:" + substring2);

    }
}
if (m3.find()) {
    System.out.println(m3.group(1));

    int test3 = m3.group(1).indexOf(".");

    if (test3 != -1) {
        substring3 = m3.group(1).substring(0, test3); //
        System.out.println("value is:" + substring3);
    }
}
if (m4.find()) {
    System.out.println(m4.group(1));

    int test4 = m4.group(1).indexOf(".");

    if (test4 != -1) {
        substring4 = m4.group(1).substring(0, test4); //
        System.out.println("value is:" + substring4);
    }
}

System.out.println("returned valueis:" + subString);
System.out.println("returned valueis:" + substring1);
System.out.println("returnedvalueis:" + substring2);
System.out.println("returnedvalueis:" + substring3);
System.out.println("returnedvalueis:" + substring4);

if (subString.matches(replacetext)) {
    System.out.println("first captcha matched");

} else if (substring1.matches(replacetext)) {
    System.out.println("second captcha matched");

} else if (substring2.matches(replacetext)) {
    System.out.println("third captcha matched");

} else if (substring3.matches(replacetext)) {
    System.out.println("fourth captcha matched");

} else if (substring4.matches(replacetext)) {
    System.out.println("fifth captcha matched");
}

} catch (Exception e) {
    e.printStackTrace();
}

}

【问题讨论】:

  • 您可能想尝试一些事情:例如,您的子字符串 replacetext 在开头有一个空格。你可以做的是使用Stringtrim 函数。此外,在匹配您可以做的事情时,将两个字符串保持在相同的情况下。或者让您的 Pattern 不区分大小写。
  • @hiren 谢谢它的工作。我使用了 trim() 函数并用 equalsIgnoreCase 替换了匹配项,我能够选择所需的验证码。再次感谢!
  • 很高兴知道它有帮助。

标签: java html selenium-webdriver


【解决方案1】:

验证码代表:

完全自动化的公共图灵测试来区分计算机和人类

因此,根据定义,解析 CAPTCHA 不能自动化:否则它无法区分计算机和人类,因此无法成为 CAPTCHA。

为了解决这个问题,你可以做一些事情——使用外部服务的API,例如http://www.deathbycaptcha.com。您实现他们的 API,向他们传递 CAPTCHA 并返回文本。

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2012-07-20
    • 2021-11-09
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多