requests module - trying to post - python

I'm using the requests module with python 3.3 and am trying to post data. This is the data I'm trying to post. I've already parsed the values from the html:
<form action="/checkoutnow/2" autocomplete="off" class="proceed" id="loginForm" method="post" name="login_form" novalidate="novalidate">
<input id="loginForm" name="execution" type="hidden" value="e21s1"> <input name="token" type="hidden" value="EC-2MJ027776Y1687721"> <input id="incontext" name="incontext" type="hidden" value="0"> <input id="eventID" name="_eventId_submit" type="hidden">
<div class="" id="loginFields">
<div class="inputField emailField confidential">
<label for="email">Email</label> <input autocomplete="off" data-validate-email="true" id="email" name="email" type="email" value="">
<div class="tip tipHint">
<div class="tipArrow tipArrowFront tipHintArrowFront"></div>
<div class="tipArrow tipArrowBack tipHintArrowBack"></div>
<ul class="tipText tipHintText">
<li>Did you mean <a class="emailLink" href=""></a>?
</li>
</ul>
</div>
</div>
<div class="inputField passwordField confidential">
<label for="password">Password</label> <input autocomplete="off" id="password" maxlength="22" name="password" type="password" value="">
<div class="toolTip tip guestTooltip">
<div class="tipArrow tipArrowFront tipErrorArrowFront"></div>
<div class="tipArrow tipArrowBack tipErrorArrowBack"></div>
<div class="tipText tipErrorText">
Trouble logging in? You can try again, or <a class='submit' href='/checkoutnow/2?execution=e21s1&_eventId_guest' id='guestTooltipLink'>check out as a guest</a>
</div>
</div>
</div>
</div>
<div class="buttons varA">
<input class="btn full continue" name="_eventId_submit" type="submit" value="Log in to PayPal">
<div class="forgotPassword secondary" id="forgotPasswordSection">
<a href='/us/merchantpaymentweb?cmd=_account-recovery&from=PayPal' id='forgotPassword' target='_blank'>Forgot your password?</a>
</div>
<hr class="sep">
<a class="btn btn-secondary submit" href="/checkoutnow/2?execution=e21s1&_eventId_guest&token=EC-2MJ027776Y1687721" id="checkoutAsAGuestBtn">Pay with Debit or Credit Card</a>
</div>
</form>
And here is my code to get past this. I am using a session btw.
payload6 = {'loginFields': {'password': paypalpass, 'email': useremail}, 'token': token, 'execution': execution, 'incontext': incontext, '_eventId_submit': ''}
r = session.post(urlnow, data=payload6)
I believe my problem is with the 'loginFields' as after I post, the values are still empty.

Your POST data shouldn't have any nested dicts. password and email should be top-level keys like token and execution are, and not nested under loginFields like they are now. (I assume loginFields is in your payload now because of that <div id="loginFields">, but wrappers like that shouldn't have any effect on how the POST data is structured.)

It turns out javascript was generating more variables. Thanks for the help though.

Related

Automation of login is not possible using Selenium (Python)

I am trying to get report from id-pwd required site. This site is showing 2 different login sites randomly (I do not know why, but I am guessing they do not like retrieving data by bot).
Login HTML 1:
<div class="login-modal-content">
<form accept-charset="UTF-8" action="/mp/login" class="signin-page-form" name="sign-in-form" method="post" novalidate="novalidate">
<div class="field email-field fade-label">
<label class="text" for="email">Email Address</label>
<input autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="autofocus" class="text" id="email" name="email" size="30" spellcheck="false" type="email" value="">
</div>
<div class="message-box-error-wrapper">
<div class="message-box-error"></div>
</div>
<div class="field password-field fade-label">
<label class="text" for="password">Password</label>
<input class="text" id="password" name="password" size="30" autocomplete="off" type="password" value="">
<div class="login-forgot-password-wrapper">
Forgot Password
</div>
</div>
<div style="clear: both"></div>
<div id="captcha" class="g-recaptcha submit-button"></div>
<div class="submit-button full-length-submit-button">
<button id="sign-in-button" name="sign-in-button" type="submit" class="btn btn-blue track-event">
<span class="button-text">Sign In</span>
</button>
</div>
<div class="footer">
Don’t have a ---- account?
<span class="signup-today">
Sign up
</span>
</div>
</form>
</div>
Login HTML 2.
<main class="layout-base__body">
<h1 data-test-heading="">Sign in</h1>
<p data-test-subheading="">
New to Square? <market-link href="/signup?country_code=us&lang_code=en" hydrated="">Sign up</market-link>
</p>
<form data-test-form="" name="sign-in-form">
<market-field data-test-email-field="" hydrated="">
<market-input-text data-test-email-input="" name="email" input-id="email" autocomplete="username" type="email" value="" hydrated="" class="">
<label for="email">Email address</label>
</market-input-text>
<!---->
</market-field>
<market-field data-test-password-field="" hydrated="">
<market-input-password data-test-password-input="" name="password" type="password" hydrated="">
<label for="password">Password</label>
</market-input-password>
<!---->
</market-field>
<p>
<div>
<market-link data-test-forgot-password="" id="login-standard-forgot-password-link" href="#" hydrated="">
Forgot password?
</market-link>
</div>
</p>
<!---->
<div>
<market-button data-test-submit="" id="login-standard-submit" name="sign-in-button" rank="primary" role="button" type="button" variant="regular" size="medium" hydrated="">
Sign in
</market-button>
</div>
</form>
</main>
Here is my partial code for login.
def find_single_name(browser, timeout, el_name):
try:
WebDriverWait(browser, timeout).until(
EC.presence_of_element_located(
(By.NAME, el_name)
)
)
element = browser.find_element(By.NAME, el_name)
return element
except TimeoutException:
print(f"Could not find element ID of {el_name}")
return None
###########
login_id = find_single_name(browser=browser, timeout=60, el_name='email')
login_id.click()
login_id.clear()
login_id.send_keys(id)
time.sleep(1)
login_pwd = find_single_name(
browser=browser, timeout=60, el_name='password')
login_pwd.click()
login_pwd.clear()
login_pwd.send_keys(pwd)
time.sleep(1)
login_btn = find_single_name(
browser=browser, timeout=60, el_name='sign-in-button')
login_btn.click()
time.sleep(timeout)
When I happened to get login HTML 1 page, no problem to sign in and download all the reports I need, but same code is not working on login HTML 2. browser closed in no time on login 2 HTML.
I am struggling with this problem for weeks and gave up to solve this issue. Please advise me. I am praying not to deal with login 2 HTML whenever scraping that site...

How to get/Fetch Form data in django using cloud firestore?

Form picture
ID is been selected but I'm not getting the values in the form, please check my code files. See in the picture in URL of the browser, update/id is selected, the problem is values are not fetched in the form.
HTML:
<form id="task-form" name="myForm">
{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-building" placeholder="Building name" name="building" value="{{buildings.building}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-postal" placeholder="Postal Code" name="postalCode" value="{{buildings.postalCode}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-town" placeholder="town" name="town" value="{{buildings.town}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-street" placeholder="Street" name="street" value="{{buildings.street}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-house" placeholder="House No." name="houseNo" value="{{buildings.houseNo}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-info" placeholder="Additional Information" name="additionalInfo" value="{{buildings.additionalInfo}}">
</div>
</div>
</div>
<div class="text-center mt-3">
<button type="submit" id="btn-task-form" class="btn btn-primary ">UPDATE</button>
</div>
</form>
views.py
def update_Building(request, id):
docId = id;
context = {
'buildings': db.collection('Buildings').document(docId).get()
}
return render(request,"EmployeeAdmin/updateBuilding.html", context)
urls.py
path('update/<str:id>/',views.update_Building,name='update_Building'),

Can I use mailto and send a form in flask app?

I've seen a few problems like this but they have a different answer than I was hoping so here I am.
I am planning to send mail details such that the client's mail app opens with the message sent via the form.
Thing is though, I've seen it being doable with PHP but I am running a Flask app.
So is there a way to do it using Python?
This is my form code:
<form id="contactForm" name="sentMessage" novalidate="novalidate" action="mailto:mail#gmail.com?subject=feedback&body=message">
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2"><label>Name</label><input class="form-control" type="text" id="name" required="" placeholder="Name"><small class="form-text text-danger help-block"></small></div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2"><label>Email Address</label><input class="form-control" type="email" id="email" required="" placeholder="Email Address"><small class="form-text text-danger help-block"></small></div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-5 pb-2"><textarea class="form-control" id="message" required="" placeholder="Message" rows="5"></textarea><small class="form-text text-danger help-block"></small></div>
</div>
<div id="success"></div>
<div class="form-group"><button class="btn btn-primary btn-xl" id="sendMessageButton" type="submit">Send</button></div>
</form>
What I am looking to do is send a subject and content that is present in the textarea element as the body.
I managed to solve it myself with a workaround using redirect and request and it worked.
#app.route('/test',methods=['POST'])
def test():
mess=request.form['textarea']
return redirect('mailto:test#email.com?body='+str(mess))
In the form what I changed was
<form id="contactForm" name="sentMessage" novalidate="novalidate" action="{{url_for('test'}}" method="POST">

How would I send and request data to a web server using python?

I've been trying to use the request library in order to send information to a web server. However, I have no clue what data to send to the server in order to log in. (This is my first time using requests and I was wondering if someone could help explain this to me)
Edit: I'm trying to make it submit a username and password to the site.
My code:
import requests
Sdata = {'username':'testingemail#gmail.com','password':'testing123','login':'submit'}
r = requests.post(url = 'https://gmchosting.com/billing/clientarea.php',data=Sdata)
html = r.text
print(html)
The Part of the website I want the file to communicate with:
<input type="hidden" name="token" value="003cc13dfa662dd183f098c2a2afc81331da0ba3" />
<div class="form-group">
<label for="inputEmail">Email Address</label>
<input type="email" name="username" class="form-control" id="inputEmail" placeholder="Enter email" autofocus>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="rememberme" /> Remember Me
</label>
</div>
<div align="center">
<input id="login" type="submit" class="btn btn-primary" value="Login" /> Forgot Password?
</div>
</form>
</div>
You can use parameters in the url, like this:
https://google.com?myparameter=true
And you can get them using JS.
So, you can use:
import webbrowser
webbrowser.open('https://example.com?myparam=hello&hello=yeah')
And in the JS:
const params = new URLSearchParams(window.location.search);
const myparam = decodeURIComponent(urlp.get('myparam'));
alert(myparam)

How to add a button in email input?

I am trying to put a button inside an email input in a Django form.
This is the code with the button next to the email input:
<div class="container" id="notifyEmailFormContainer">
<form action="{% url "landing:notify_email_add" %}" method="post" id="notifyEmailForm">
{% csrf_token %}
<div class="form-group row">
<div class="col-sm-10 input-group" id="emailInput">
<div class="input-group-addon" id="comingSoonEmailIcon"><i class="fa fa-envelope fa fa-2x" aria-hidden="true"></i></div>
<input class="form-control" type="email" name="email" placeholder="Your email...." maxlength="255" required id="id_email"/>
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-block btn-primary" onclick="addNotifyEmail()" id="submitNotifyEmail">Notify Me</button>
</div>
</div>
</form>
</div>
I have a tried a couple answers in similar questions but it seems I can't get it to work.

Categories