Xpath提取部分字符串

时间:2016-04-19 07:44:56

标签: html regex selenium xpath selenium-webdriver

这是我要从中提取的HTML。

$(document).ready(function () {
    $("input#PicButton").click(function () {
        $.ajax({
            type: "GET",
            url: "ajax/get1.php",
            dataType: "json",
            data: self.dataElements,
            success: function(msg){
                console.log(msg[0]);
                       }
             });
        });
});

我需要一个Xpath来仅提取状态值,即<input type="hidden" id="continueTo" name="continueTo" value="/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state=72a37ba7-2033-47f4-8e7e-69a207406dfb" />

3 个答案:

答案 0 :(得分:2)

您好,您只能使用xpath而不是其子字符串获取属性的值,但如果您想获得子字符串,请按以下方式执行

String AttributeValue = driver.findElement(By.id("continueTo")).getAttribute("value");
System.out.println("Value of the attribute value is : " + AttributeValue);

    // now as you want 72a37ba7-2033-47f4-8e7e-69a207406dfb this substring of the vale attribute 
    // then plz apply java split as below 
    String value = "/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state=72a37ba7-2033-47f4-8e7e-69a207406dfb";
    String [] myValue = value.split("=");
    System.out.println(myValue[0]); // will print /oauth2/authz/?response_type
    System.out.println(myValue[1]); // will print code&client_id
    System.out.println(myValue[2]); // will print localoauth20&redirect_uri
    System.out.println(myValue[3]); // will print http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state
    System.out.println(myValue[4]); // will print 72a37ba7-2033-47f4-8e7e-69a207406dfb

希望这有助于您的查询

答案 1 :(得分:1)

XPATH:

//input[@id="continueTo"]/@value

它将获取标识为input的节点continueTo的值。然后需要首先使用Regex处理它以获得最终结果。

正则表达式:

`[^=]+$`

$表示字符串的结尾。它将获取字符串末尾的所有内容,而不是=

答案 2 :(得分:1)

使用以下代码: -

    String val = driver.findElement(By.xpath("//input[@id='continueTo']/@value")).getText();

    String [] myValue = val.split("&state=");

    System.out.println(myValue[1]);

希望它会对你有所帮助:)。