简体   繁体   中英

parse html with restassured

For my restassured response I receive HTML that looks like

<html>
  <body>
     <div class="col-md-5 no-left-padding">
          <select class="form-control selectpicker" id="organizationId" name="organizationId" data-live-search="true" data-none-results-text="Nothing found">
                <option value="3">OptionName</option>
                <option value="12">AnotherOptionName</option> 

Is there a better way to parse it to get a map of option values and option names other than working with it as a string?

PS I can get options via Selenium but I don't want to use it here either

When you get your response as String, you can use Jsoup to parse it and extract needed value:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import static org.assertj.core.api.Assertions.assertThat;

...

Document doc = Jsoup.parse(stringHtmlFromReponse, "UTF-8", "");
Elements links = doc.select("option");
assertThat(doc.select("option").attr("value")).isEqualTo("3");
assertThat(doc.select("option").get(0).text()).isEqualTo("OptionName");

More Jsoup capabilities you can find here or in official documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM