1

I have this form which contains a button with some dynamic value, when I click on it it should add the product to the checkout page. Here is the html form :

    {%for p in product %}
 <div class="single-product">
    <div class="product-f-image">
      <img src="data:image/png;base64,{{p.image_medium}}" alt="">
         <div class="">
       {% if not user.is_authenticated %}
      <form action="/login/">
       <button class="add-to-cart-link" type="submit"> Add to cart</button>
      </form>
      {%else%}
    <form id="form-id"  action="" method="POST">
    {% csrf_token %}
  <button class="add-to-cart-link" type="submit" name="product" value="{{p.id}}" >
<input type="hidden" name="product_name" value="{{p.name}}">
<input type="hidden" name="product_price" value="{{p.lst_price}}">
    Add to cart</button>
  </form>
     {%endif%}
 <a href="#" class="view-details-link"><i class="fa fa-link"></i> See details</a>
 </div>
  </div>
 <h2><a href="single-product.html">{{p.id}}</a></h2>
<div class="product-carousel-price">
 <ins>{{p.lst_price}} €</ins>
                                </div>

                            </div>
                            {%endfor%}

And here is my what I am doing with selenium:

bon_commande = self.selenium.find_element_by_xpath("//button[@name='product' and @value='37']/parent::form")
bon_commande.submit()

And thanks for the help !

3 Answers 3

1

You don't need to locate Submit button to submit a form - use any element inside form or form element itself:

self.selenium.find_element_by_id("form-id").submit()

self.selenium.find_element_by_class_name("add-to-cart-link").submit()

Update

Try to wait until django variable "{{p.id}}" is replaced with generated value:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(self.selenium, 10).until(EC.presence_of_element_located((By.XPATH, "//button[@name='product' and @value='37']"))).submit()
Sign up to request clarification or add additional context in comments.

4 Comments

Ok ! this got me to the checkout page but no product added :/ i think we need to pass the id of the product
Do you need to click something like "Add to cart" button before submitting form? Can you share the sequence of manual steps you need to execute to get required output?
Sure ! I've got a list of product (e-commerce website), the 'add-to-cart' button contains some element like (id, name, price). When a user click on the button of the desired product its add to the checkout page. Hope I was clear
So there are many "Add to cart" buttons on page - each inside its own form. And after clicking button new page is opened? Can you share little more HTML?
0

Change to click the submit button:

// add some sleep to wait the JS files of page 
// load completely to register click event to the submit button
// otherwise nothing to response to the click 
// (because the `action` of the form is empty.)

self.selenium.sleep(15); // sleep 15 seconds
self.selenium.find_element_by_xpath("//button[@name='product' and @value='37']").click()

2 Comments

Thanks, there is no error but still, it didn't send me to the page of checkout which mean I think the click button didn't perform ! :/
Try add some sleep before click the button
0

To click on the button with text as Add to cart you can use the following line of code :

self.selenium.find_element_by_xpath("//form[@id='form-id']/button[@class='add-to-cart-link' and @name='product']").submit()
#or
self.selenium.find_element_by_xpath("//form[@id='form-id']/button[@class='add-to-cart-link' and @name='product']").click()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.