2017-09-23 39 views
0

我的方案是搜索任何品牌的筆記本電腦,我需要選擇當前頁面中的最高價格,而無需排序。自動Flipkart應用程序使用硒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(); 
} 

我在eclipse控制檯中打印了當前頁面中的所有價目表。 但我不知道如何點擊最高價格的筆記本電腦。 請幫我解決這個問題。

回答

1

如果你想點擊最高價格搜索您需要執行以下兩件事情:

#1評論下面幾行:

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

#2更換searchHPComputers()方法體用以下代碼

 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(); 

and voila !!

UPDATE

@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

我需要選擇性價比最高的筆記本電腦在當前頁面不排序。 –

+0

@MohanKumar:嘗試更新的部分 – kushal

+0

它的工作...... !!!!!!!!謝謝。 –