I have a html file which gets form fields from a FlaskForm. The code is:
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">
{% if title %}
<title>Xpress Products - {{ title }}</title>
{% else %}
<title>Xpress Products</title>
{% endif %}
</head>
<body>
<header class="site-header">
<nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="/">Xpress Products</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToggle">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" href="{{ url_for('xpress') }}">Home</a>
<a class="nav-item nav-link" href="{{ url_for('storestock') }}">Stock</a>
<a class="nav-item nav-link" href="{{ url_for('order') }}">Place Order</a>
<a class="nav-item nav-link" href="{{ url_for('amend') }}">Amend Order</a>
<a class="nav-item nav-link" href="{{ url_for('cancel') }}">Cancel Order</a>
<a class="nav-item nav-link" href="{{ url_for('addproduct') }}">Add Products</a>
</div>
</div>
</div>
</nav>
</header>
<main role="main" class="container">
<div class="row">
<div class="col-md-12">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block content %}
<div>
<form method="POST" action="">
{{ form.hidden_tag() }}
<div class="content-section">
<fieldset class="form-group">
<legend class="border-bottom mb-4">Add new Product</legend>
<div class="row">
<div class='col-md-4'>
<div class="form-group">
{{ form.newproduct.label(class="form-control-label") }}
{{ form.newproduct(class="form-control form-control-lg") }}
</div>
</div>
<div class='col-md-2'>
<div class="form-group">
{{ form.pricepsmall.label(class="form-control-label") }}
{{ form.pricepsmall(class="form-control form-control-lg") }}
</div>
</div>
<div class='col-md-2'>
<div class="form-group">
{{ form.pricepmedium.label(class="form-control-label") }}
{{ form.pricepmedium(class="form-control form-control-lg") }}
</div>
</div>
<div class='col-md-2'>
<div class="form-group">
{{ form.priceplarge.label(class="form-control-label") }}
{{ form.priceplarge(class="form-control form-control-lg") }}
</div>
</div>
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</div>
</form>
</div>
{% endblock content %}
</div>
</div>
</main>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
I am making a unit test using selenium. The code is:
class TestProductStore(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(60)
def tearDown(self):
self.driver.quit()
def test_form_addproduct(self):
self.driver.get('http://127.0.0.1:5000/addproduct')
newproduct_input = self.driver.find_element_by_name('form.newproduct')
pricepsmall_input = self.driver.find_element_by_name('form.pricepsmall')
pricepmedium_input = self.driver.find_element_by_name('form.pricepmedium')
priceplarge_input = self.driver.find_element_by_name('form.priceplarge')
# Populate inputs with dummy text
newproduct_input.send_keys('Rich Blend Teabag Carton')
pricepsmall_input.send_keys(15.0)
pricepmedium_input.sendkeys(18.0)
priceplarge_input.sendkeys(21.0)
# Find submit button and submit form by sending an "Enter" keypress
submit_button = self.driver.find_element_by_css_selector('.btn-outline-info')+
submit_button.send_keys(Keys.ENTER)
admin_url = self.driver.current_url
self.assertEqual(admin_url, 'http://localhost:5000/addproduct')
theproduct = Products.objects.get(product_desc='Rich Blend Teabag Carton')
self.assertEqual(theproduct.product_desc, 'Rich Blend Teabag Carton')
running the test throws the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="form.newproduct"]"}
Could someone please guide me how to rephrase the code to enable detection of the form fields. Help will be greatly appreciated. Just to let you know, I also tried to find elements without the form prefix, like for example,
newproduct_input = self.driver.find_element_by_name('newproduct')
This also does not work
Tried to search by xpath:
newproduct_input = self.driver.find_element_by_xpath('//*[@id="newproduct"]')
but still same error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//[@id="newproduct"]"}*