获取body标签内的特定值的文本

时间:2015-11-05 11:22:01

标签: selenium selenium-webdriver

我在企业消息传递解决方案中试用Api(msg91):获取Balance api显示余额:

回应:

  

[{ “平衡”:1000, “路径”: “4”, “类型”: “SMS”}]

我想从Balance获取文字。我如何使用网络驱动程序获取它。

2 个答案:

答案 0 :(得分:1)

  1. 创建余额等级:

    @Getter
    @Setter
    public class Balance {
      private String balance;
      private String route;
      private String type;
    }
    
  2. 创建一个BalancePage类,使用webDriver导航页面,读取响应并将其解析为json:

    public class BalancePage extends Page {
      private List<Balance> balances;
    
      @FindBy(xpath = "html/body")
      WebElement response;
    
      BalanceConfig config = create(BalanceConfig.class);
    
      protected BalancePage(TestWebDriver driver) {
        super(driver);
        testWebDriver.setBaseURL(config.url() + "?authkey=" + config.authKey());
        PageFactory.initElements(new AjaxElementLocatorFactory(testWebDriver, 30), this);
    }
    
      private String getBalanceString() {
        return response.getText();
      }
    
      public void getCurrentBalance() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        balances = objectMapper.readValue(getBalanceString(), objectMapper.getTypeFactory().constructCollectionType(List.class, Balance.class));
      }
    } 
    
    public abstract class Page {
    
      public WebDriver testWebDriver;
    
      protected Page(WebDriver driver) {
        this.testWebDriver = driver;
       }
    }
    

答案 1 :(得分:0)

简单字符串

string[] res = response.Split(",")

string balance = new String(res[0].ToCharArray().Where(c => Char.IsDigit(c)).ToArray());