-
-
Save leoncio44/1b2595ab0fed24c861ddb176eb748a01 to your computer and use it in GitHub Desktop.
A Selenium WebDriver HTMLUnit example for WhatIsMyIp
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
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