Posted By

jerryvig on 09/03/11


Tagged

java Firefox prices selenium


Versions (?)

Scrape Geeks.com Prices with FirefoxDriver for Selenium RC


 / Published in: Java
 

This is a simple Java program to collect PC pricing data from Geeks.com using the Firefox driver in Selenium RC. The use of the FirefoxDriver class permits much easier parsing of the HTML DOM tree versus the use of the DefaultSelenium class. The data are printed to a CSV file using opencsv.

http://selenium.googlecode.com/svn/tags/selenium-2.5.0/docs/api/java/index.html

http://opencsv.sourceforge.net/

  1. import org.openqa.selenium.firefox.FirefoxDriver;
  2. import au.com.bytecode.opencsv.CSVWriter;
  3. import java.io.IOException;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.util.List;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.By;
  9.  
  10. public class GeeksDotCom {
  11. public static void main( String[] args ) {
  12. try {
  13.  
  14. CSVWriter myWriter = new CSVWriter( new FileWriter( "./GeeksDotCom.csv" ) );
  15.  
  16. FirefoxDriver driver = new FirefoxDriver();
  17.  
  18. driver.get( "http://www.geeks.com/products.asp?cat=SYS" );
  19.  
  20. myWriter.writeNext( new String[]{ "Description", "Price" } );
  21.  
  22. List<WebElement> tableList = driver.findElementsByTagName("table");
  23.  
  24. for ( WebElement myTable : tableList ) {
  25. String description = "";
  26. String price = "";
  27. try {
  28. if ( (myTable.getAttribute("bordercolor")).equals("#CCCCCC") ) {
  29. if ( myTable.getAttribute("class").equals("txt11px") ) {
  30.  
  31. List<WebElement> aList = myTable.findElements( By.tagName("a") );
  32. for ( WebElement anchor : aList ) {
  33. if ( anchor.getAttribute("href").contains("details.asp") ) {
  34. if ( anchor.getAttribute("class").equals("productlink") ) {
  35. description = anchor.getText().trim();
  36. }
  37. }
  38. }
  39.  
  40. List<WebElement> spanList = myTable.findElements( By.tagName("span") );
  41. for ( WebElement span : spanList ) {
  42. if ( span.getAttribute("class").equals("checkoutprice") ) {
  43. price = span.getText().trim();
  44. }
  45. }
  46. }
  47. }
  48. } catch ( NullPointerException npe ) { npe.printStackTrace(); }
  49. if ( description.contains( "GB" ) ) {
  50. myWriter.writeNext( new String[]{ description, price } );
  51. }
  52. }
  53.  
  54. myWriter.close();
  55.  
  56. }
  57. catch ( Exception e ) { e.printStackTrace(); }
  58. }
  59. }

Report this snippet  

You need to login to post a comment.