Skip to main content

Buy MI Phone : Scripting Flipkart

This is sample code for buying MI ( Xiaomi ) Phone using selenium scripts on Flipkart.

Things to keep in mind :
  • Find working ( as today : 30/12/14 ) visual studio project here :  Download
  • You will need chrome installed on your system
  • In this project we have used reference to selenium webdriver.
  • If Flipkart change Tag name or Class name in future, We may need to change accordingly. Current html for 'Buy Now' button is included in code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace BuyMIPhoneFlipkartScripted
{
    class Program
    {
        static void Main(string[] args)
        {
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--start-maximized");
            IWebDriver driver = new ChromeDriver(options);

            // This Will Login Into FLipkart
            driver.Navigate().GoToUrl("https://www.flipkart.com/account/login");
            IWebElement email = driver.FindElement(By.Id("login_email_id1"));
            email.SendKeys("yourEmailID@gmail.com");
            IWebElement pass = driver.FindElement(By.Id("login_password1"));
            pass.SendKeys("YourPassWord");
            pass = driver.FindElement(By.Id("login-cta"));
            System.Threading.Thread.Sleep(4999);
            pass.Click();

            // Sleep is to get in sync, You can incrase this , If your connection is slow
            System.Threading.Thread.Sleep(9999);

            // Below url is which Phone to buy, Every MI Phone have different UI
            driver.Navigate().GoToUrl("http://www.flipkart.com/mi/note-4g?otracker=_tabs_note-4g");
            long attemp = 0;
            bool click = false;

            // Will Wait for input , Enter Key only when time remaining is 1 or 2 minutes.
            Console.WriteLine("Enter any key to continue attempting");
            Console.ReadKey();
            while (!click)
            {
                attemp++;
                Console.WriteLine("Attempt No. : " + attemp );
                try
                {
                    IWebElement buy;
                    try
                    {
                        // Try to get button using exact class name
                        buy = driver.FindElement(By.CssSelector("div.jbv.jbv-orange.jbv-buy-big.jbv-reserve")); // Working
                    }
                    catch (Exception w)
                    {
                        // Try to get button using text in class name
                        buy = driver.FindElement(By.XPath("//*[contains(@class, 'buy')]"));
                    }
                    Console.WriteLine(buy.Text);
                    try
                    {
                        buy.Click();
                        click = true;
                    }
                    catch (Exception e)
                    {
                        //This is for getting error message
                        // Can be commented if it annoys you
                        Console.WriteLine("Click =  " + e.Message);
                    }
                    try
                    {
                        // Because Sometimes Click doesn't work  , Can't take risk na ;)
                        buy.Submit();
                        click = true;
                    }
                    catch (Exception e1)
                    {
                        //This is for getting error message
                        // Can be commented if it annoys you
                        Console.WriteLine("Submit" + e1.Message);
                    }
                    if (click)
                    {
                        // If Clicked, It will exit.
                        Console.WriteLine("Successful Clik Bitch !!");
                        break;
                    }
                }
                catch (Exception e)
                {
                    //This is for getting error message
                    // Can be commented if it annoys you
                    Console.WriteLine(e.Message);
                }
            }
            /*
            * <div class="gd-row">
                    <div class="gd-col gu16">
                        <div class="emailModule message module-tmargin">
                            <div class="error-msg"></div>
                            <div class="register brdr-btm">
                                <div class="jbv jbv-orange jbv-buy-big jbv-reserve">Buy Now</div>
                            </div>
                            <div class="topTextWrap brdr-btm tmargin20">
                                <div class="subHeading">
                                    Only one phone per registered user
                                    <p>
                                    First come, first serve!
                                    </p>
                                    </div>
                                </div>
                            </div>
                      </div>
                </div>
            */
        }
    }
}




Feel free to comment or drop a mail to writetomansa@gmail.com.


Comments

Popular posts from this blog

Drawing and Animating ASCII Art in C# Console

This is sample code for drawing ASCII art character in C# console. Things to keep in mind : Replace the variable var with any ASCII art you want to get drawn in console.  Take care of [@],["] and [,] while replacing. using System ; namespace Dotned.UI.Framework { public class Arbit { public static void Main (String[] Args) { var arr = new [] { @" /$$$$$$ /$$ /$$ /$$ " , @" /$$__ $$ | $$ | $$ |__/ " , @" | $$ \__/ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ /$$$$$$ | $$$$$$$ /$$ /$$$$$$$ /$$$$$$ " , @" | $$$$$$ /$$__ $$| $$_ $$_ $$ /$$__ $$|_ $$_/ | $$__ $$| $$| $$__ $$ /$$__ $$ " , @" ...

Git – setting up a remote repository and doing an initial push

So, Lets firstly set-up the remote repository: Step 1 : ssh will get me access to console on remote pc, will ask for remote pc's password Step 2 : create a directory with extension .git Step 3 : go to created directory Step 4 : initialize git Step 5 : Update Server configuration for access from non local sites Step 6 : Exit from remote access                     $ssh username@ipaddressofserverpc                    $mkdir my_project.git                    $cd my_project.git                    $git init --bare                    $git update-server-info # If planning to serve via HTTP                    $exit Now, On local machine: Step 1 : go to local folder Step 2 : Initia...

Serializing & Deserializing JSON Object [ C++ ]

         This is sample code for serializing an object into json object and deserializing it back into earlier object. Things to keep in mind : While creating a json object, you have to write  json_object* jObj=json_object_new_object();   initializing it as   json_object* jObj ;   will not work. You need to install JSON library using  $ sudo apt-get install libjson0 libjson0-dev, Check path of installed json here   $ ls /usr/include/json You need to add json library to linker or compiler path. For ex. in Eclipse , Right click on project >> properties >> path and symbols. Add /usr/include/json in library paths & json in libraries. //============================================================================ // Name : JsonTest.cpp // Author : Manish Kumar Khedawat //============================================================================ #include <iostream> #include <json...