Currently I am testing my flask app using selenium. So I have an html with simple FlaskForm in it which contains submit button. Of course I can click on it and submit a form using
btn = driver.find_element_by_id("submit-btn").click()
But the problem is that click method doesn't return anything.
So my question is how can I get at least the response code of submitted form so I can be sure that my tests run ok?
No, click() does not return anything, and you cannot get response code by selenium as well.
You need to verify test based on what the web displays, like success alert, text or something.
Related
Basically I am currently writing a Python Requests program and everything works fine so far, The issue is there is a button on the website that I want to be pressed. Currently the clicking stage is done in selenium which slows the process down completely.
Just like when filling in a form is there anyway to pass a parameter into a URL to click a button?
The button sends a POST command to a URL when clicked however when trying to recreate this in a browser or in python with .post the desired redirect does not happen.
Any help is much appreciated.
I have written a python function using flask framework to process some data submitted via a web form. However I don't want to re-render the template, I really just want to process the data and the leave the web form, it the state it was in, when the POST request was created. Not sure how to do this ... any suggestions ?
If you want the user to stay in place, you should send the form using JavaScript asynchronously. That way, the browser won't try to fetch and render a new page.
You won't be able to get this behavior from the Flask end only. You can return effectively nothing but the browser will still try to get it and render that nothing for the client.
In flask programming, people usually use 'url_for' such as {{url_for = 'some url'}}
This way have to make URL(#app.route), template(HTML) and map each other.
But, I just want to send email when I click submit button in HTML modal.
For this, There is no page reloading. To do this, I think I have to connect between button and python function without URL, return(response)
I wonder that how to make it, help me please, I'm beginner in flask programming.
You can't trigger anything on the server without making a request to a URL.
If you don't want the page to reload, you can either redirect back to the original page after your action is finished, or you can use Ajax to make the request without changing the page; but the request itself is always to a URL.
I'm trying to fill forms of an ajax box (just my term for those several forms) using the mechanize module, but it seems not to work. I'm not a web programmer but afaik the ajax box updates itself 'onchange' with an event which is handled by the browser.
Mechanize seems not to handle that, in the links list (from the iterator Browser.links) I can find a url 'javascript:AjaxRetry();' with an error msg as text which tells me that something has gone wrong.
Here is my code:
import mechanize as m
br = m.Browser()
br.open(url)
br.select_form(nr=0)
# fill in one form (in a real browser, the other form refresh and are not disabled anymore)
br.set_value(code, br.form.controls[10].name)
# how to make it refresh now?
#br.submit() doesn't work (also br.click() does not work (no clickable around at all))
Is mechanize the right module to fill forms of that ajax box?
I can't paste the link to the page where that ajax box is, because you have to be logged-in in order to see that box.
Mechanize doesn't handle javascript, see this answer for more details and an alternative solution How to properly use mechanize to scrape AJAX sites
So I have an authenticated site that I want to access via the mechanize module. I'm able to log in, and then go to the page I want. However, because the page recognizes that mechanize doesn't have javascript enabled, it wants me to click a submit button to get redirected to a non javascript part of the site. How can I simply click the button and then read the contents of the page that follows that?
Or, is there a way to trick it into thinking that my javascript is enables?
Thanks!
if that submit button is really a submit input element of the form, and the redirection works as usual form submit action, and provided that it's the only form in the page, your mechanize browser instance is br, following should work
br.select_form(nr=0) # select the first form
br.submit()
afaik, there's no simple or moderately possible way, how to emulate javascript in mechanize, possible workarounds depend on what is javascript exactly doing