1

I'm currently doing some task for my university. I'm writing Django Selenium tests for my app. I'm trying to write test for deleting object.

They key parts of my code are:

delete confirmation.html

<form action="" method="post" id="confirmForm">{% csrf_token %}
    <p>{% trans "Are you sure you want to delete this car?" %}</p>
    <input class="center-block btn btn-danger" type="submit" value="{% trans 'Confirm' %}"/>
</form>

modal.js

function showModal(url) {
    $("#myModalBody").text();
    $("#myModal").modal();
    $.ajax({
        url,
        cache: false
    }).done(function (html) {
        $("#myModalBody").html(html);
        $("#confirmForm").attr('action', url);
    });

}

When I click following button

<button class="btn btn-primary btn-sm pull-left" value="" id="car-delete-modal-btn m-10-b" onclick="showModal('{% url 'c2crental:delete_car' details_car.id %}');">{% trans "Delete car" %}</button>

Modal appears

And test stops with result: Unable to locate element. I was trying to use different selectors but none of them worked. For now test looks like this:

# click 'delete' button to display popup confirmation window
delete_btn = self.selenium.find_element_by_id('car-delete-modal-btn m-10-b')
delete_btn.click()

# click 'confirm' to delete object in popup window
submit = 'input[type="submit"]'
confirm_btn = self.selenium.find_element_by_id(submit)
confirm_btn.click()

I just can't seem to find proper selector for this button or it's a problem with a synchronization. Can someone help me fix it?

Sorry for my broken english and i hope you will understand what I'm trying to do here but I'm just starting with Django, tests, programming in general so not everything is clear.

2 Answers 2

1

@PixelEinstein 's Answer was in the right direction however I suppose the WebDriverWait will timeout as the node input[@type="submit"] is not the immediate child node of [@id="confirmForm"].

Perhaps, you can induce WebDriverWait with the following Locator Strategy :

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.element_to_be_clickable((By.XPATH, "//form[@id='confirmForm']//input[@class='center-block btn btn-danger']"))).click()
#or
WebDriverWait(self.selenium, 10).until(EC.element_to_be_clickable((By.XPATH, "//form[@id='confirmForm']//input[@class='center-block btn btn-danger']"))).submit()
Sign up to request clarification or add additional context in comments.

Comments

1

These lines are causing your problem:

submit = 'input[type="submit"]'
confirm_btn = self.selenium.find_element_by_id(submit)

You are trying to pass CSS to an id selector.

I would try adding this before trying to click on the submit:

# Make sure to import these first
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.element_to_be_clickable((By.XPATH, './/*[@id="confirmForm"]//input[@type="submit"]'))

Then try this click instead of id, XPATH:

self.selenium.find_element_by_xpath('.//*[@id="confirmForm"]//input[@type="submit"]').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.