How to click on particular button?.

Hi,

How to click on particular button?, when the all identifiers are same?.

Please find the below Screenshot, HTML and application URL.

Image:

How to click on particular button?.

HTML:


<input type="submit" class="wpsc_buy_button" name="Buy" value="Add To Cart">

URL:
http://store.demoqa.com/products-page/product-category/accessories/

Doctor Asked on September 7, 2014 in Selenium WebDriver.
Add Comment
  • 6 Answer(s)
    Best answer

    Hi Rocky,

    You can use the Page object Model to handle it dynamically, please find the code below:

    
    public class ProductListing_Page extends BaseClass {
    private static WebElement element;
    public ProductListing_Page(WebDriver driver){
    
    super(driver);
    
    }
    public static class Product_1{
    public static WebElement btn_AddToCart(){
    
    element = null;
    
    try{
    
    element= driver.findElement(By.xpath(".//*[@id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"));
    
    Log.info("Add to Cart button is found for Product 1");
    
    }catch (Exception e){
    
    Log.error("Product 1 Add to Cart button is not found");
    
    throw(e);
    
    }
    
    return element;
    
    }
    
    }
    public static class Product_2{
    public static WebElement btn_AddToCart(){
    
    element = null;
    
    try{
    
    element= driver.findElement(By.xpath(".//*[@id='default_products_page_container']/div[4]/div[2]/form/div[2]/div[1]/span/input"));
    
    Log.info("Add to Cart button is found for Product 2");
    
    }catch (Exception e){
    
    Log.error("Product 2 Add to Cart button is not found");
    
    throw(e);
    
    }
    
    return element;
    
    }
    
    }
    }
    
    

    Once you done with that, you can use the above code with the following code:

    
    ProductListing_Page.Product_1.btn_AddToCart.click();
    
    

    And if there are many items you are intrested in during your test, then it would be great if you just create one method for ‘Add to Cart’  and pass the [div] number as an argument to the method.

    
    public class ProductListing_Page extends BaseClass {
    private static WebElement element;
    public ProductListing_Page(WebDriver driver){
    
    super(driver);
    
    }
    public static class Product{
    public static WebElement btn_AddToCart(int Index){
    
    element = null;
    
    int iIndex = Index;
    
    try{
    
    element= driver.findElement(By.xpath(".//*[@id='default_products_page_container']/div["+iIndex+"]/div[2]/form/div[2]/div[1]/span/input"));
    
    Log.info("Add to Cart button is found for Product");
    
    }catch (Exception e){
    
    Log.error("Product Add to Cart button is not found");
    
    throw(e);
    
    }
    
    return element;
    
    }
    
    }
    }
    
    

    Above code can be used like this:

    
    ProductListing_Page.Product_2.btn_AddToCart(3).click();
    
    

    Best Regards,
    Lakshay Sharma

    Professor Answered on September 9, 2014.

    Ufff.. Now I do not have any road blocks. Accepted.

    on September 9, 2014.
    Add Comment

    One of the many answer is: use Xpath in this condition 🙂 , for example.
     

    
    .//*[@id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input
    .//*[@id='default_products_page_container']/div[4]/div[2]/form/div[2]/div[1]/span/input
    

    In another question I saw that you are working on MDF and I think you required generic code, but sorry friend I don’t know that 🙁 .

    Other people will suggest you the different solutions..

    Vice Professor Answered on September 7, 2014.
    Add Comment

    hi
    Use Indexing in Your Xpath
    example
    xpath=(//input[@value=’add to cart’])[index number]

    Check Index Number For Your Element on Firepath

    thanks
    ankit

    Vice Professor Answered on September 8, 2014.

    Thanks Ankit, I can also write xpath but I am looking for generic code because the reason is this will be useful when dealing with eCommerce application. Here we will be having lot of buttons so. But thanks for your time.

    Thanks for your time Ankit.

    on September 8, 2014.
    Add Comment

    When you give the xpath  and two buttons have same id , by default selenium takes the first button.  you can give starts with or contains or index number

    Doctor Answered on September 9, 2014.
    Add Comment

    Xpath – (“(//input[@name=’Buy’])[1]”) for the first button

    you can change the index number for each product buy now button and you can use them

    Train Answered on September 15, 2014.
    Add Comment

    If you have something in common with all the buttons then you can write a generic code and using indexing and then using for loop with equals or contains methods to find the specific object at each iteration.

    Default Answered on March 7, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.