Skip to content

Instantly share code, notes, and snippets.

@leoncio44
Forked from djangofan/CheckSite.java
Created February 18, 2018 15:39
Show Gist options
  • Save leoncio44/1b2595ab0fed24c861ddb176eb748a01 to your computer and use it in GitHub Desktop.
Save leoncio44/1b2595ab0fed24c861ddb176eb748a01 to your computer and use it in GitHub Desktop.
A Selenium WebDriver HTMLUnit example for WhatIsMyIp
package tr.qa;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class CheckSite {
public static void main( String[] args )
{
System.out.println( "Hello World!" );
// An example of using HtmlUnitDriver
String msg = "\nMy External IP Address Is" + "\n";
HtmlUnitDriver driver = new HtmlUnitDriver(true);
driver.get("http://checkip.dyndns.com/");
String ipstring = driver.findElement( By.cssSelector("html body") ).getText();
System.out.println( "Debug: " + ipstring ); // just a debug statement to print out result
msg = msg + ipstring.substring( ipstring.indexOf(": ") + 2) + "\n";
// then, print the output to the console
System.out.println( msg );
// then, write the information to a file
FileWriter outFile = null;
try {
outFile = new FileWriter("ip.txt");
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter out = new PrintWriter(outFile);
out.write(msg);
out.close();
try {
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment