Created
June 15, 2012 01:33
-
-
Save dimiro1/2934117 to your computer and use it in GitHub Desktop.
Correios HtmlUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.gargoylesoftware.htmlunit.BrowserVersion; | |
import com.gargoylesoftware.htmlunit.WebClient; | |
import com.gargoylesoftware.htmlunit.html.*; | |
import java.io.IOException; | |
public class CorreiosCep { | |
private static final String URL = "http://www.buscacep.correios.com.br/"; | |
private static final String CEP = "64011010"; | |
public static void main(String args[]) throws IOException { | |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); | |
// Não precisamos de Javascript, ActiveX e CSS | |
webClient.setJavaScriptEnabled(false); | |
webClient.setActiveXNative(false); | |
webClient.setCssEnabled(false); | |
final HtmlPage searchPage = webClient.getPage(URL); | |
// Encontra o Formulário | |
final HtmlForm form = searchPage.getFormByName("Geral"); | |
// Botão de Submit | |
final HtmlElement button = form.getHtmlElementsByTagName("button").get(0); | |
// Campo de CEP | |
final HtmlTextInput relaxation = form.getInputByName("relaxation"); | |
relaxation.setText(CEP); | |
// Tipo de Busca | |
final HtmlSelect tipoCep = form.getSelectByName("TipoCep"); | |
tipoCep.setDefaultValue("ALL"); | |
final HtmlRadioButtonInput semelhante = form.getInputByName("semelhante"); | |
semelhante.setChecked(true); | |
// Campos Ocultos | |
final HtmlHiddenInput cfm = form.getInputByName("cfm"); | |
cfm.setValueAttribute("1"); | |
final HtmlHiddenInput metodo = form.getInputByName("Metodo"); | |
metodo.setValueAttribute("listaLogradouro"); | |
final HtmlHiddenInput tipoConsulta = form.getInputByName("TipoConsulta"); | |
tipoConsulta.setValueAttribute("relaxation"); | |
final HtmlHiddenInput startRow = form.getInputByName("StartRow"); | |
startRow.setValueAttribute("1"); | |
final HtmlHiddenInput endRow = form.getInputByName("EndRow"); | |
endRow.setValueAttribute("10"); | |
// Submit Form | |
final HtmlPage resultPage = button.click(); | |
// Encontra a Tabela com o resultado | |
final HtmlTable resultTable = (HtmlTable) resultPage.getByXPath(".//*[@id='lamina']/div[2]/div[2]/div[2]/div[2]/table[1]").get(0); | |
System.out.println(resultTable.asText()); | |
webClient.closeAllWindows(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment