0

i am trying to find the "Buy Now" element in the flipcart site.i have tried many times with various methods but i am getting either InvalidSelectorException or NoSuchElementException. help me in locating the element.Here is my script

    WebDriver d=new FirefoxDriver();
    d.get("http://www.flipkart.com/books");
    d.findElement(By.cssSelector("div[href*='children']")).click();
    d.findElement(By.cssSelector("img[alt='children_2-5_Years']")).click();
    d.findElement(By.cssSelector("div[data-pid='9788184514414'>.list-unit>.gd-row>.gu9>.gd-row:eq(1)>.gu4>div:eq(4)>.btn-orange")).click();

Thanks in advance

2
  • Can u tell me which book, u want to select? If it is fine for u using XPATH, i can help you.
    – HemaSundar
    Nov 5, 2014 at 11:01
  • my goal is to click on the Buy Now button given corresponding to the book.by using this selector d.findElement(By.cssSelector("input[value='Buy Now']")) the first Buy now button in the page is getting clicked.for ex if i want to buy the 4th book in the list i have to click the buy now btn given for that book.i am uable to find selector for that. Nov 5, 2014 at 11:22

2 Answers 2

0
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/books");
driver.findElement(By.linkText("Children")).click();//Click Children from left pane
driver.findElement(By.xpath("//img[@title='children_2-5_Years']")).click();//Click 2-5 years old image link
driver.findElement(By.xpath("//div[@id='products']/div/div[2]//input[@value='Buy Now']")).click();//click BuyNow for 2nd book

In the Above code, in the last statement, change the div number to click on different Buy now buttons.

3
  • Thanks for ur solution.d.findElement(By.xpath("//div[@id='products']/div/div[2]//input[@value='Buy Now']")) worked i am able to click on the btn for a specific book.but the other two selectors are not working still getting InvalidSelectorException.could u please explain why Nov 5, 2014 at 14:26
  • Updated the comment. If it is working fine, Accept the answer, So that it will be helpful to others.
    – HemaSundar
    Nov 5, 2014 at 14:36
  • it is working fine with xpath selector but not with the other two selectors Nov 5, 2014 at 15:02
0

Please use like d.findElement(By.id("elementid")).click();

If selector does not work then you can easily use Id or xpath to get element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.