Untitled

 avatar
unknown
plain_text
2 years ago
2.8 kB
5
Indexable
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FlipkartAutomation {

    public static void main(String[] args) {
        // Set the browser path
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver(); // or FirefoxDriver()

        // Open Flipkart website
        driver.get("https://www.flipkart.com/");
        
        // Handle pop-ups (if any)
        // Add appropriate code here
        
        // Search for mobiles under 15000
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("mobiles");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ac-row"))).click();
        
        // Apply price filter and select OS version
        WebElement priceFilter = driver.findElement(By.xpath("//div[@class='_1YoBfV']//select[@class='_2YxCDZ']"));
        Select select = new Select(priceFilter);
        select.selectByValue("10000");
        WebElement osVersion = driver.findElement(By.xpath("//div[text()='Operating System']//following-sibling::div//input"));
        osVersion.sendKeys("Pie");
        
        // Sort by newest first
        WebElement sortDropdown = driver.findElement(By.className("Z3MXg"));
        Select sortBy = new Select(sortDropdown);
        sortBy.selectByVisibleText("Newest First");
        
        // Display first five mobiles' Name and Price
        for (int i = 1; i <= 5; i++) {
            WebElement mobileName = driver.findElement(By.xpath("(//div[@class='_4rR01T'])[" + i + "]"));
            WebElement mobilePrice = driver.findElement(By.xpath("(//div[@class='_30jeq3 _1_WHN1'])[" + i + "]"));
            System.out.println("Mobile " + i + " - Name: " + mobileName.getText() + ", Price: " + mobilePrice.getText());
        }
        
        // Validate first mobile's price < 30000
        WebElement firstMobilePrice = driver.findElement(By.xpath("(//div[@class='_30jeq3 _1_WHN1'])[1]"));
        String priceString = firstMobilePrice.getText().replaceAll("[^0-9]", ""); // Remove non-numeric characters
        int price = Integer.parseInt(priceString);
        if (price < 30000) {
            System.out.println("First mobile price is less than Rs. 30000");
        } else {
            System.out.println("First mobile price is not less than Rs. 30000");
        }
        
        // Close the browser
        driver.quit();
    }
}
Editor is loading...
Leave a Comment