This is sample code for buying MI ( Xiaomi ) Phone using selenium scripts on Flipkart.
Things to keep in mind :
Feel free to comment or drop a mail to writetomansa@gmail.com.
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
Post a Comment