SHARE
TWEET

Example 2 – How to get all links of selected area of webpage

SeleniumETrainR Dec 21st, 2012 249 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.List;
  2. import java.util.concurrent.TimeUnit;
  3.  
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8.  
  9.  
  10. public class ExampleQuikrClassifiedAdsonline {
  11.  
  12.          
  13.         public static void main(String[] args) {
  14.                 WebDriver driver = new FirefoxDriver();
  15.                 driver.navigate().to("http://www.quikr.com");
  16.                
  17.                 driver.findElement(By.xpath("//*[@id='chpBoxCloseButton']")).click();
  18.  
  19.                 driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
  20.                 List<WebElement> link_boxes=driver.findElements(By.xpath("//*[@id='cats']"));
  21.                 System.out.println("Number of boxes "+link_boxes.size() );
  22.                
  23.                
  24.                
  25.                 for(int j=0;j<link_boxes.size();j++){
  26.                        
  27.                         WebElement box = link_boxes.get(j);
  28.                         List<WebElement> links = box.findElements(By.tagName("a"));
  29.                         System.out.println("Total links for---"+link_boxes.get(j+1)+"---are--- "+links.size() );
  30.                         System.out.println("====================================="+j);
  31.                         for(int i=0 ; i<links.size();i++){
  32.                                 System.out.println("*********************************************");
  33.                                 System.out.println(links.get(i).getText());
  34.                                 System.out.println(links.get(i).getAttribute("href"));
  35.                                
  36.                         }
  37.                 }
  38.  
  39.                 driver.quit();
  40.  
  41.         }
  42.  
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top