2017-09-23 6 views
0

Mon scénario consiste à rechercher n'importe quel ordinateur portable de marque et je dois sélectionner le prix le plus élevé dans la page en cours sans trier.Automatisation de l'application Flipkart à l'aide de Selenium WebDriver

@BeforeClass 
public void launchBrowser() 
{ 
    System.setProperty("webdriver.chrome.driver", "F:\\chromedriver_win32\\chromedriver.exe"); 
    driver = new ChromeDriver(); 
    driver.manage().window().maximize(); 
    driver.get("https://www.flipkart.com"); 
} 

    @Test(priority = 0) 
    public void searchComputers() throws InterruptedException 
    { 
    WebDriverWait wait = new WebDriverWait(driver, 40); 
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@title='Search for Products, Brands and More']"))); 
    driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys("laptops"); 
    driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys(Keys.ENTER); 
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']"))); 
    Thread.sleep(1000); 
    JavascriptExecutor jse = (JavascriptExecutor)driver; 
    jse.executeScript("window.scrollBy(0,250)", ""); 
    driver.findElement(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']")).click(); 
} 

@Test(priority = 1) 
public void searchHPComputers() throws InterruptedException 
{ 
    Thread.sleep(2000); 
    WebDriverWait wait1 = new WebDriverWait(driver, 40); 
    wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']"))); 
    Thread.sleep(1000); 
    List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']")); 
    for(WebElement web : element) 
    { 
    String amount = web.getText(); 
    int length = amount.length(); 
    String price = amount.substring(1, length); 
    System.out.println("Amount : "+ price); 
    Thread.sleep(1000); 
    } 

} 


@AfterClass 
public void closeBrowser() throws InterruptedException 
{ 
    Thread.sleep(2000); 
    driver.close(); 
    driver.quit(); 
} 

J'ai imprimé toute la liste de prix dans la page actuelle dans la console eclipse. Mais je n'ai aucune idée de comment cliquer sur le portable le plus cher. Veuillez m'aider à résoudre ce problème.

Répondre

1

Si vous voulez cliquer sur la recherche avec le prix le plus élevé que vous devez effectuer suivant 2 choses:

# 1 Commentaire lignes suivantes:

JavascriptExecutor jse = (JavascriptExecutor)driver; 
    jse.executeScript("window.scrollBy(0,250)", ""); 

# 2 remplacer le corps de la méthode searchHPComputers() avec le code suivant

 Thread.sleep(5000); 
     driver.findElement(By.xpath("//li[text()='Price -- High to Low']")).click(); 

     Thread.sleep(5000); 
     List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']")); 

     element.get(0).click(); 

et le tour est joué !!

MISE À JOUR

@Test(priority = 1) 
public void searchHPComputers() throws InterruptedException 
{ 

    Thread.sleep(2000); 
    WebDriverWait wait1 = new WebDriverWait(driver, 40); 
    wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']"))); 
    Thread.sleep(1000); 
    List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']")); 
    List<WebElement> elementLink = driver.findElements(By.xpath("//div[@class='_3wU53n']")); 

    int largestPrice =0, elementIndex =0, i=0; 

    for(WebElement web : element) 
    { 
     String amount = web.getText(); 
     int length = amount.length(); 
     String price = amount.substring(1, length); 
     price = price.replaceAll(",", ""); 
     int priceInt = Integer.parseInt(price); 
     System.out.println("Amount : "+ priceInt); 
     Thread.sleep(1000); 

     if(priceInt > largestPrice) { 

      largestPrice = priceInt; 

      elementIndex = i; 
     } 

     i++; 
    } 


    JavascriptExecutor jse = (JavascriptExecutor)driver; 
    jse.executeScript("arguments[0].scrollIntoView(true);", elementLink.get(elementIndex-1)); 

    WebDriverWait wait = new WebDriverWait(driver, 20); 
    wait.until(ExpectedConditions.elementToBeClickable(elementLink.get(elementIndex))); 

    elementLink.get(elementIndex).click(); 
} 
+0

je dois choisir l'ordinateur portable de prix le plus élevé dans la page en cours sans tri. –

+0

@MohanKumar: essayez la partie mise à jour – kushal

+0

Cela a fonctionné ... !!!!!!!! Je vous remercie. –