Advertisement
SeleniumETrainR

Example - find product from product list and click on produc

Dec 19th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  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 testexamplevirtuemart {
  11.  
  12.     public static void main(String[] args) throws InterruptedException {
  13.         WebDriver myTestDriver = new FirefoxDriver();
  14.         myTestDriver.manage().window().maximize();
  15.        
  16.         myTestDriver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
  17.         myTestDriver.get("http://tinyurl.com/brnygh4");
  18.        
  19.         // fill List with all total no of products link
  20.         List<WebElement> AllMobilePhonesLinks = myTestDriver.findElements(By.xpath("//div[@class='spacer']/h3/a"));
  21.         System.out.println("Total No of products ------"+ AllMobilePhonesLinks.size());
  22.        
  23.         //print all mobile names on console
  24.         for(int i=0;i<AllMobilePhonesLinks.size();i++){
  25.            
  26.         System.out.println(AllMobilePhonesLinks.get(i).getText());
  27.            
  28.         }
  29.        
  30.         // Fill list with all Product details links for each phone
  31.         List<WebElement> AllMobilePhonesButtons = myTestDriver.findElements(By.xpath("//div[@class='product-link']/a[@class='btn btn-small']"));
  32.         System.out.println(AllMobilePhonesButtons.size());
  33.        
  34.        
  35.         // Loop through all product names to get the desired product
  36.        
  37.         for(int i=0;i<AllMobilePhonesButtons.size();i++){
  38.            
  39.              // Click on Product when name of the product is matched with "i9000 Galaxy S"
  40.             if (AllMobilePhonesLinks.get(i).getText().equals("i9000 Galaxy S") ) {
  41.                
  42.                 AllMobilePhonesButtons.get(i).click();
  43.                 break;
  44.             }
  45.            
  46.             Thread.sleep(50000L);
  47.            
  48.             myTestDriver.quit();
  49.            
  50.     }
  51.  
  52.  
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement