I want to change the HTML code to Selenium code - python

Original HTML Code
<form id="commentUpdateForm" method="post" data-commentid="">
<fieldset>
<input type="hidden" name="point" value="">
<div class="grade_star">
<span class="ico_star star_rate">
<span class="ico_star inner_star" style="width: 0%;"></span>
</span>
<em class="num_rate">0<span class="txt_g">/5</span></em>
<span class="txt_word">Please rate it</span>
</div>
<div class="write_review">
<label for="tfReview" class="lab_review">What did you like about this place?</label>
<textarea name="contents" id="tfReview2" class="tf_review"></textarea>
</div>
</fieldset>
</form>
If I click 3 out of 5 points, the HTML code will be changed like this.
<form id="commentUpdateForm" method="post" data-commentid="">
<fieldset>
*<input type="hidden" name="point" value="3">*
<div class="grade_star">
<span class="ico_star star_rate">
*<span class="ico_star inner_star" style="width: 60%;"></span>*
</span>
<em class="num_rate">0<span class="txt_g">/5</span></em>
<span class="txt_word">Normal</span>
</div>
<div class="write_review">
<label for="tfReview" class="lab_review">What did you like about this place?</label>
<textarea name="contents" id="tfReview2" class="tf_review"></textarea>
</div>
</fieldset>
</form>
So what I want to do is to make it possible to modify it with Python Selenium code for points, but I don't know.

You can locate the element with selenium webdriver and modify its value attribute like that:
element = driver.find_element_by_xpath('//form[#id="commentUpdateForm"]//input[name="point"]')
element.setAttribute("value", "3")

Related

can't select radio buttons on multiple forms

So, I have multiple jobs in view with "Report" modal. in that modal there is a form with reasons to report. The problem is that radio button highlights only in first form. if I open report for fifth job it will change first job report modal and there will be no button highlight, but if you open modal of first job, its highlight. I made sure to change name= attribute of every group, so I don't understand why it's happening
<!-- Modal report posting -->
<div class="modal" id="{{post.id}}-1" tabindex="-1" role="dialog" aria-labelledby="validate-modal-label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Report</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{{post.id}}
<div class="modal-body">
<form action="" class="report_job_form" method="post" novalidate>
<input type="hidden" name="crf-protect" id="crf-protect" value="{{csrf_token()}}">
<div class="report-element">
<div class="report-category hoverable">
<p>
<label for="fkj">
<input type="radio" id="fkj" name="report{{post.Position}}_value" value="Fake Job">
<span>Fake Job</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="disc-3">
<input type="radio" id="disc-3" name="report{{post.Position}}_value" value="Discrimination">
<span>Discrimination</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="advert">
<input type="radio" id="advert" name="report{{post.Position}}_value" value="Advertisement">
<span>Advertisement</span>
</label>
</p>
</div>
<div class="report-category hoverable">
<p>
<label for="other-2">
<input type="radio" id="other-2" name="report{{post.Position}}_value" value="Other">
<span>Other</span>
</label>
</p>
</div>
</div>
<div class="input-field col s12">
<textarea id="textarea7" class="materialize-textarea" data-length="120" name="details"></textarea>
<label for="textarea2">More Details</label>
</div>
<input type="hidden" name="user-ident" id="user-ident" value="{{current_user.id}}" readonly>
<input type="hidden" name="post-ident" id="post-ident" value="{{post.id}}" readonly>
<input class="btn report-submit" type="submit">
</form>
</div>

How can i pass the table values to modal in Django?

When the edit option is clicked in the project, I want to transfer all the values in the relevant row into the modal, how can I do this?
here table body
<tbody class="list form-check-all">
{% for x in model %}
<tr>
<td class="id">{{x.name}}</td>
<td class="company_name">{{x.phonenumber}}</td>
<td class="leads_score">{{x.note}}</td>
<td class="phone">{{x.status}}</td>
<td class="location">{{x.callname}}</td>
<td class="date">{{x.dataname}}</td>
<td>
<ul class="list-inline hstack gap-2 mb-0">
<li class="list-inline-item" data-bs-toggle="tooltip" data-bs-trigger="hover" data-bs-placement="top" title="Edit">
<a class="edit-item-btn" href="#showModal" data-bs-toggle="modal"><i class="ri-phone-line fs-16"></i></a>
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
and here my modal
<div class="modal fade" id="showModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-light p-3">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="close-modal"></button>
</div>
<form action="">
<div class="modal-body">
<input type="hidden" id="id-field" />
<div class="row g-3">
<div class="col-lg-12">
<div>
<label for="leadname-field" class="form-label">Name</label>
<input type="text" id="leadname-field" class="form-control" placeholder="Enter Name" required />
</div>
</div>
<!--end col-->
<div class="col-lg-12">
<div>
<label for="company_name-field" class="form-label">Company Name</label>
<input type="email" id="company_name-field" class="form-control" placeholder="Enter company name" required />
</div>
</div>
<!--end col-->
<div class="col-lg-6">
<div>
<label for="leads_score-field" class="form-label">Leads Score</label>
<input type="text" id="leads_score-field" class="form-control" placeholder="Enter lead score" required />
</div>
</div>
<!--end col-->
<div class="col-lg-6">
<div>
<label for="phone-field" class="form-label">Phone</label>
<input type="text" id="phone-field" class="form-control" placeholder="Enter phone no" required />
</div>
</div>
<!--end col-->
<div class="col-lg-12">
<div>
<label for="location-field" class="form-label">Location</label>
<input type="text" id="location-field" class="form-control" placeholder="Enter location" required />
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="hstack gap-2 justify-content-end">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Kapat</button>
<button type="submit" class="btn btn-success" id="add-btn">Kaydet</button>
</div>
</div>
</form>
</div>
</div>
</div>
My codes are like this. When the edit option is clicked from here, I want to inherit the values of the relevant row in the table within the modal. How can I do that?
so basically I would do that this way - when you load modal, the element that triggers it has data-id attribute and you send it over AJAX to the Django view. Then, before the modal's fully open, you change html code of the whole div, prepopulated with single-instance template that you send over using JsonResponse.
Something like that should work:
const loadDetails = (e) => {
let btn = $(e.currentTarget);
const modalAjax = $("#modal-ajax");
$.ajax({
url: btn.attr("data-href"),
type: 'GET',
data: {id: <here_you_put_obj_id_using_data_attr>}
dataType: 'json',
beforeSend: function () {
modalAjax.modal("show");
},
success: function (data) {
modalAjax.find('.modal-content').html(data['html_form']);
}
});
};

Django - Embed python inside HTML to trigger two form actions

All I want to do is to have a condition in order when the user clicks FOR SALE button to be redirected to url 'salesearch' or when clicks TO RENT to be redirected to url 'rentsearch'.
Here's the code:
<div class="search">
<form action="{% url 'salesearch' %}">
<div class="form-row">
<div class="col-md-4 mb-3">
<label class="sr-only">
Keywords
</label>
<input class="form-control" name="keywords" placeholder="Keyword (Pool, Garage, etc)" type="text">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">
City
</label>
<input class="form-control" name="city" placeholder="City" type="text">
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<button class="btn btn-secondary btn-block mt-4" name="For Sale" type="submit">
For Sale
</button>
</div>
<div class="col-md-6 mb-3">
<button class="btn btn-secondary btn-block mt-4" name="To Rent" type="submit">
To Rent
</button>
</div>
</div>
</div>
</form>
</div>
With the above example I am redirected always at url 'salesearch path. Is there any way to have a conditional to determine the url's path?
Sure is!
You can set formaction on button elements:
<button name="For Sale" type="submit" formaction="{% url 'salesearch' %}">
For Sale
</button>
<button name="To Rent" type="submit" formaction="{% url 'rentsearch' %}">
To Rent
</button>

Failed to log into Google using Python

Can't figure out why Google won't let me log into my account. I put my email inside a variable called my_mail then search for it in the html but the results came back false.
I am using the requests & beautifulsoup modules to do so.
What I have:
from bs4 import BeautifulSoup
import requests
form_data={'Email': 'ExampleEmail#gmail.com', 'Passwd': 'Password'}
post = "https://accounts.google.com/signin/challenge/sl/password"
my_mail = 'ExampleEmail#gmail.com'
with requests.Session() as s:
soup = BeautifulSoup(s.get("https://accounts.google.com/ServiceLogin?elo=1").text, "html.parser")
for inp in soup.select("#gaia_loginform input[name]"):
if inp["name"] not in form_data:
form_data[inp["name"]] = inp["value"]
s.post(post, form_data)
print(my_mail in "https://mail.google.com/mail/u/0/#inbox")
Form info:
<form novalidate="" method="post" action="https://accounts.google.com/signin/challenge/sl/password" id="gaia_loginform">
<input name="Page" type="hidden" value="PasswordSeparationSignIn">
<input type="hidden" name="GALX" value="c90FYb-yd6E">
<input type="hidden" name="gxf" value="AFoagUVMILWKQvqlGV0i8sJG83jiQlrWzg:1475424777331">
<input id="profile-information" name="ProfileInformation" type="hidden" value="">
<input type="hidden" id="_utf8" name="_utf8" value="☃">
<input type="hidden" name="bgresponse" id="bgresponse" value="js_disabled">
<input type="hidden" id="pstMsg" name="pstMsg" value="1">
<input type="hidden" id="dnConn" name="dnConn" value="">
<input type="hidden" id="checkConnection" name="checkConnection" value="youtube:230:1">
<input type="hidden" id="checkedDomains" name="checkedDomains" value="youtube">
<div class="form-panel first valid" id="gaia_firstform">
<div class="slide-out ">
<div class="input-wrapper focused">
<div id="identifier-shown">
<div>
<label class="hidden-label" for="Email">
Enter your email</label>
<input id="Email" type="email" value="" spellcheck="false" name="Email" placeholder="Enter your email" autofocus="">
<input id="Passwd-hidden" type="password" spellcheck="false" class="hidden">
</div>
</div>
<span role="alert" class="error-msg" id="errormsg_0_Email"></span>
</div>
<div style="display: none" id="identifier-captcha">
<input type="hidden" name="identifiertoken" id="identifier-token" value="">
<input type="hidden" name="identifiertoken_audio" id="identifier-token-audio">
<div class="audio-box">
<div id="playIdentifierAudio"></div>
</div>
<div id="captcha-box" class="captcha-box">
<div id="captcha-img" class="captcha-img" data-alt-text="Visual verification">
</div>
<span class="captcha-msg">
Letters are not case-sensitive
</span>
</div>
<label for="identifier-captcha-input" class="hidden-label"></label>
<input type="text" id="identifier-captcha-input" name="identifier-captcha-input" class="captcha" placeholder="Enter the letters above" title="Type the characters you see or numbers you hear">
</div>
<input id="next" name="signIn" class="rc-button rc-button-submit" type="submit" value="Next">
<a class="need-help" href="https://accounts.google.com/signin/usernamerecovery?hl=en">
Find my account
</a>
</div>
</div>
<a href="https://accounts.google.com/ServiceLogin" tabindex="-1">
<img id="back-arrow" class="back-arrow " aria-label="Back" tabindex="0" alt="Back" src="https://www.gstatic.com/images/icons/material/system/1x/arrow_back_grey600_24dp.png">
</a>
<div class="form-panel second">
<div class="slide-in hide-form">
<div>
<p id="profile-name"></p>
<span id="email-display"></span>
</div>
<div>
<div id="password-shown"></div>
</div>
<input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Sign in">
<label class="remember">
<input id="PersistentCookie" name="PersistentCookie" type="checkbox" value="yes" checked="checked">
<span>
Stay signed in
</span>
<div class="bubble-wrap" role="tooltip">
<div class="bubble-pointer"></div>
<div class="bubble">
For your convenience, keep this checked. On shared devices, additional precautions are recommended.
Learn more
</div>
</div>
</label>
<input type="hidden" name="rmShown" value="1">
<a id="link-forgot-passwd" class="need-help" href="https://accounts.google.com/signin/recovery?hl=en">
Forgot password?
</a>
</div>
</div>
<span id="inge" style="display: none" role="alert" class="error-msg">
Sorry, Google doesn't recognize that email. Create an account using that address?
</span>
<span id="timeoutError" style="display: none" role="alert" class="error-msg">
Something went wrong. Check your connection and try again.
</span>
</form>
I don't plan on using Google's API. I don't want to be restricted.
Why is the login failing? What could I do to fix it?
I need to login to scrap some information that requires a login.
Pyhton 3.4.2

Submitting Form on OKCupid using RoboBrowser

I am trying to write python code which does the following:
1) Logs into OKCupid
2) Goes to a users questions page
3) Answers an unanswered question.
I am using RoboBrowser to do this. I can do steps 1), 2) and I can get the form of the question I want to submit, but once I submit (using RoboBrowser's submit) it doesn't seem to go through to OKCupid (in that the question isn't registered as answered on my profile).
Here is the code I have. Note the My_RoboBrowser just allows me to pass False to RoboBrowser.open for the verify argument.
class My_RoboBrowser(RoboBrowser):
def __init__(self, auth=None, parser=None, headers=None, user_agent=None, history=True):
RoboBrowser.__init__(self, parser=None, user_agent=None, history=True)
def Open(self, vURL, vVerify=True):
response = self.session.get(vURL, verify=vVerify)
self._update_state(response)
browser = My_RoboBrowser()
urlL = 'https://okcupid.com/login'
browser.open(url)
form = browser.get_form(id='loginbox_form')
form['username'] = 'Username'
form['password'] = 'Password'
browser.submit_form(form)
urlQ = 'https://www.okcupid.com/profile/USER/questions?low=1'
browser.open(urlQ)
Question_Tag = browser.find_all(class_="not_answered")[0]
ID = Question_Tag.get('data-qid')
#Get the form to fill out
Form = browser.get_form(id='answer_'+str(ID))
Form['my_answer'].value = '1'
Form['their_answer'].value = ['1']
Form['importance'].value = '1'
browser.submit_form(Form)
Also if it is helpful when I look at the form object Form in an IPython notebook it says:
<RoboForm my_answer=, their_answer=[], importance=>
before the submit and
<RoboForm my_answer=1, their_answer=['1'], importance=1>
after.
Finally, if it is helpful, the following is the code for one of the forms I am trying to answer (which was gotten by inspecting the element)
<form id="answer_179268" name="answer_179268" class="answer_area okform initialized">
<div class="container my_answer">
<input id="my_answer_1_179268" name="my_answer" value="1" false="" type="radio">
<label class="radio" for="my_answer_1_179268">
<span class="icon"></span>
Yes
</label>
<input id="my_answer_2_179268" name="my_answer" value="2" false="" type="radio"></input>
<label class="radio" for="my_answer_2_179268">
<span class="icon"></span>
No
</label>
</div>
<div class="container acceptable_answers">
<div class="title">
<p>Answer(s) you’ll accept</p>
</div>
<label class="checkbox acceptable_answer" for="their_answer_1_179268">
<input id="their_answer_1_179268" class="acceptable_answer" name="their_answer" value="1" false="" type="checkbox"></input>
<span class="icon"></span>
Yes
</label>
<label class="checkbox acceptable_answer" for="their_answer_2_179268">
<input id="their_answer_2_179268" class="acceptable_answer" name="their_answer" value="2" false="" type="checkbox"></input?
<span class="icon"></span>
No
</label>
<label class="checkbox irrelevant" for="their_answer_any_179268">
<input id="their_answer_any_179268" class="irrelevant" name="their_answer" value="irrelevant" type="checkbox"></input>
<span class="icon"></span>
Any of the above
</label>
</div>
<div class="container importance">
<div class="title">
<p>Importance</p>
</div>
<div class="importance_radios">
<input id="importance_179268_5" name="importance" value="5" false="" type="radio"></input>
<label class="importance_5 radio" for="importance_179268_5" data-count="5">
<span class="icon"></span>
<div class="bar"></div>
<span class="label"></span>
</label>
<input id="importance_179268_4" name="importance" value="4" false="" type="radio"></input>
<label class="importance_4 radio" for="importance_179268_4" data-count="4">
<span class="icon"></span>
<div class="bar"></div>
<span class="label">A little</span>
</label>
<input id="importance_179268_3" name="importance" value="3" false="" type="radio"></input>
<label class="importance_3 radio" for="importance_179268_3" data-count="3">
<span class="icon"></span>
<div class="bar"></div>
<span class="label">Somewhat</span>
</label>
<input id="importance_179268_2" name="importance" value="2" false="" type="radio"> </input>
<label class="importance_2 radio" for="importance_179268_2" data-count="2">
<span class="icon"></span>
<div class="bar"></div>
<span class="label"></span>
</label>
<input id="importance_179268_1" name="importance" value="1" false="" type="radio"> </input>
<label class="importance_1 radio" for="importance_179268_1" data-count="1">
<span class="icon"></span>
<div class="bar"></div>
<span class="label">Very</span>
</label>
</div>
<div class="irrelevant_message">
<span class="irrelevant_text">Irrelevant</span>
<span class="message_text">(Because you’ll accept any answer, this question is marked irrelevant)</span>
</div>
</div>
<div id="explanation_container_179268" class="container explanation">
<div id="answer_179268_explanationContainer" class="inputcontainer textarea noresize empty">
<textarea id="answer_179268_explanation" class="noresize" placeholder="Explain your answer (optional)" false=""></textarea>
<span class="message empty" style="height: 0"></span>
<div class="icon"></div>
</div>
</div>
<button id="submit_btn_179268" class="submit_btn flatbutton disabled small">Answer</button>
<button id="cancel_btn_179268" class="cancel_btn flatbutton silver small">Cancel</button>
<a class="skip_btn inner" href="javascript:void(0)" draggable="false">Skip question</a>
<div id="public_container_179268" class="answer_privately">
<label class="checkbox" for="private_179268">
<input id="private_179268" false="" type="checkbox"></input>
<span class="icon"></span>
<span class="text">Answer privately</span>
</label>
</div>
</form>
I've seen several cases where someone couldn't submit b/c the site used Javascript to submit forms. In other words, they could use Robobrowser or Mechanize to log in and fill out a form, but the form itself could not be submitted -- as submission relied on JS. That may be your problem here. If that's the case, you should try using Selenium.
You can likely verify whether JS is being used to submit in your browser's inspector. <ctrl + shift + i / network / *clear* inspector's network panel before clicking submit / click submit / check type for your Post>
I think that's how I verified, but others here would know better than me. Good luck!
You should take a look at https://github.com/IvanMalison/okcupyd. It will let you do this without using a browser.

Categories

Resources