Let me start by apologizing for my utter newbness. I was asked by a friend a couple years ago if I could write a program to automatically grab substitute teaching openings. It wasn't an area I knew anything about, but a couple tutorials allowed me to bang something out despite ignorance about html (and more than a little about Python for that matter). Script worked great since then but this year their site seems to have been redone and broke things, pushing it far beyond my understanding.
My previous code that worked:
# Create a Browser instance
b = mechanize.Browser()
# Load the page
b.open(loginURL)
# Select the form
b.select_form(nr=0)
# Fill out the form
b['id'] = 'XXXXXXXXXX' # Note: I edited out my friend's login info here for privacy
b['pin'] = 'XXXX'
b.submit();
There is still only one form but the controls are now of type "hidden" and are not the ones I directly need any longer. I can see the old fields in the html when I examine it with developer mode and the names are the same but I can't figure out (tried some things that didn't work) how I would access them now. Here is the html:
<form id="loginform" name="loginform" method="post" action="https://www.aesoponline.com/login.asp?x=x&&pswd=&sso=">
<input type="hidden" name="location" value="">
<input type="hidden" name="qstring" value="">
<input type="hidden" name="absr_ID" value="">
<input type="hidden" name="foil" value="">
<div style="margin: auto; text-align:center;">
<div id="loginContainer" style="text-align: left;">
<div id="loginContent">
<div id="Div1" style="position:relative; left:65px;" class="hide-me-for-rebranding">
<a href="http://www.frontlinetechnologies.com">
<img src="images/frontlinelogo.png" border="0">
</a>
</div>
<div id="loginLoginBox" style="position:relative;">
<div id="loginAesopLogo" style="padding-bottom:0px;" class="hide-me-for-rebranding"></div>
<!--endloginAesopLogo-->
<div id="loginLoginFields" style="margin-top:0px;">
<br>
<table>
<tbody>
<tr height="25px">
<td width="30px"><span class="corrLoginFormText">ID:</span>
</td>
<td>
<input type="text" class="loginFormText" maxlength="80" id="txtLoginID" name="id" value="">
</td>
</tr>
<tr height="25px">
<td width="30px"><span class="corrLoginFormText">Pin:</span>
</td>
<td>
<input type="password" class="loginFormText" maxlength="20" id="txtPassword" name="pin">
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr height="30px">
<td width="75px" valign="top">
<a class="textButton" id="loginLink" name="loginLink" href="#"><span style="white-space:nowrap;">Login</span></a>
<input type="hidden" id="submitLogin" name="submitLogin" value="1">
</td>
<td>
<div id="loginhelp" style="float:right;">
<img src="images/icon.pinreminder.png" alt="pin" width="10" height="15" align="top">Pin Reminder
<br>
<img src="images/icon.loginproblems.png" alt="login" width="11" height="17" align="top"> Login Problems
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!--endloginLoginFields-->
<div id="errorLabel" style="position: absolute; top: 170px; left:5px;margin:0px;"><span class="assistanceText"></span>
</div>
</div>
<!--endloginLoginBox-->
<div id="loginContentText">
<span class="loginContentHeader">Welcome To Absence Management</span>
<br>
<span class="loginContentText">
You are about to enter Frontline Absence Management!<br> Please enter your ID and PIN to login to your account, or click the button below to learn more about Frontline's growing impact on education.</span>
<br>
<a class="textButton" href="http://www.frontlinek12.com/Products/Aesop.html"><span>Learn More</span></a>
</div>
<!--endloginContentText-->
</div>
<!--endLoginContent-->
<div id="loginFooterShading" class="hide-me-for-rebranding">
<div id="loginFooterLeft"></div>
<div id="loginFooterRight"></div>
</div>
<!--endloginFooterShading-->
<div id="loginFooter" style="text-align:center;width:725px;">
Privacy Policy
<br>© Frontline Technologies Group LLC <
<parm1>>
<br>All rights reserved. Protected under US Patents 6,334,133, 6,675,151, 7,430,519, 7,945,468 and 8,140,366 with additional patents pending.
</parm1>
</div>
<!--endloginReflections-->
</div>
<!--endLoginContainer-->
</div>
<!--end margin div -->
<!-- MODAL DIALOG -->
<div id="basicModalContent" style="display:none">
<span class="assistanceText"></span>
</div>
</form>
Any assistance would be greatly appreciated. Thank you very much.
Try this :
b = mechanize.Browser()
b.set_handle_equiv(False)
b.set_handle_robots(False)
b.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:18.0)Gecko/20100101 Firefox/18.0 (compatible;)'),('Accept', '*/*')]
b.open(loginURL).read()
b.select_form(nr=0)
b['id'] = 'XXX'
b['pin'] = 'XXX'
resp = b.submit()
print resp.read()
It works for me!
Try something like this if that html code is exactly what is on that page. When you put b.select_form(nr=0) there is the possibility that for some reason the first form is not what you are selecting. By looking for the form name in the b.select_form() you can ensure you find the correct form. Test it out and see if it works out.
import mechanize
b = mechanize.Browser()
b.open(loginURL)
#since the actual form is named loginform just select it
b.select_form("loginform")
b['id'] name of login input field
b['pin']
b.submit()
Related
I have a table which contains for loop and if tag using jinja this table shows some data but in last column I want round toggle button but I only get a checkbox, I am unable to find the error please help me.
<tbody>
{%for student in students%}
{%if user.staff.class_coordinator_of == student.division and user.staff.teacher_of_year == student.year%}
<tr>
<td style="color:white;">{{student.user.first_name}}</td>
<td style="color:white;">{{student.user.last_name}}</td>
<td style="color:white;">{{student.year}}</td>
<td style="color:white;">{{student.division}}</td>
<td style="color:white;">{{student.batch}}</td>
<td>
<label class="switch ">
<input type="checkbox" id="" value="" checked>
<span class="slider round"></span>
</label>
</td>
</tr>
{% endif %}
{%endfor%}
</tbody>
OUTPUT
Output Image
You should use radio button instead of checkbox so, it should be type="radio".
<input type="radio" id="" value="" checked>
There are many questions and answers for selecting multiple options in a Multi-select dropdown, but few, if any, that deal with only reading currently selected options.
How can the "active" options be read from the following Multi-select dropdown? Note that Widget A and Widget C are active and Widget B is not.
It seems like you would use Select.all_selected_options. Xpath to the multi-select-container is //*[#id="addActivityTable"]/tbody/tr[17]/td[2]/div/ul.
HTML
<table id="addActivityTable" width="100%" border="0" cellspacing="0" cellpadding="0" class="table">
<tbody>
<tr>
<td><b> Vendor Approved Widgets</b></td>
<td colspan="3">
<select name="WidgetStatus_DD" id="WidgetStatus_DD" class="text2" multiple="multiple" style="width: 200px; display: none;">
<option value="multiselect-all"> Select all</option>
<option value="123" selected=""> WIDGET A</option>
<option value="456"> WIDGET B</option>
<option value="789" selected=""> WIDGET C
<div class="btn-group open">
<button type="button" class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" title=" WIDGET A
, WIDGET B
" aria-expanded="true">2 selected <b class="caret"></b></button>
<ul class="multiselect-container dropdown-menu" mylist="" data-input="#filtersearch" data-autodividers="true" data-inset="true" style="max-height: 200px; overflow: hidden auto;">
<div class="input-group">
<span class="input-group-addon" style="padding: 4px 9px;">
<img src="/css/e003.jpg">
</span>
<input id="filtersearch" class="form-control multiselect-search" type="text" placeholder="Search">
</div>
<li>
<a href="javascript:void(0);" class="multiselect-all">
<label class="checkbox">
<input type="checkbox" value="multiselect-all">
" Select all"
</label>
</a>
</li>
<li class="active">
<a href="javascript:void(0);">
<label class="checkbox">
<input type="checkbox" value="123"> WIDGET A
</label>
</a>
</li>
<li>
<a href="javascript:void(0);">
<label class="checkbox">
<input type="checkbox" value="456"> WIDGET B
</label>
</a>
</li>
<li class="active">
<a href="javascript:void(0);">
<label class="checkbox">
<input type="checkbox" value="789"> WIDGET C
</label>
</a>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
This was my attempt that fails:
def get_selected_values_from_dropdown(self, xpath):
try:
select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath))))
return select.all_selected_option.get_attribute("value")
except NoSuchElementException as e:
print(f"Dropdown element at {xpath} not found.")
xpath = '//*[#id="addActivityTable"]/tbody/tr[17]/td[2]/div/ul'
record = dict()
if key == "widgets":
widgets = self.get_selected_values_from_dropdown(xpath)
record[key] = ", ".join(widgets)
To get the active option elements you can use this:
active_options = driver.find_elements_by_xpath("//li[#class='active']//label")
UPD
After you updated the HTML.
I can't be sure since I can't see the other dropdowns, but if this specific dropdown block unique locator is id="addActivityTable" the code above will be
active_options = driver.find_elements_by_xpath("//table[id='addActivityTable']//li[#class='active']//label")
or maybe
active_options = driver.find_elements_by_xpath("//table[id='addActivityTable']//td[#colspan='3']//li[#class='active']//label")
Also, possibly you can do it in some other way, like this:
active_options = driver.find_elements_by_xpath("//table[id='addActivityTable']//td[#colspan='3']//option[#selected]")
To give a certain answer I still need to see the other dropdowns on that page
use the following code on twitter and github it work fine but it doesn't work on the main site i am using it for, can someone please tell me what went wrong.
i did not get any error instead it scrap back the login page instead of log me.
import requests
session = requests.Session()
params = {'j_username': '**********', 'j_password': '************'}
r = requests.post("https://connect.data.com/loginProcess", params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------")
print("Going to profile page...")
r = requests.get("https://connect.data.com/home")
print(r.text)
I am new to this so i can't figure out what went wrong, i have try many answer out before asking the question but none seem to workout for the site.
The login url is /login but the file that the login is been process is /loginProcess that is why i use /loginProcess but loginProcess does not print out my cookie but /login does print it out
The form to the site look like this:
<form id="command" name="LoginForm" action="https://connect.data.com/loginProcess" method="post">
<div>
<div class="login-container fields float-left">
<div class="content">
<div class="first">
<span class="title">Login</span>
</div>
<div class="middle">
<input id="j_username" name="j_username" type="email" class="text" placeholder="Email" maxlength="128" tabindex="1">
</div>
<div class="middle">
<input id="j_password" name="j_password" type="password" class="text" placeholder="Password" autocomplete="off" tabindex="2" maxlength="128">
</div>
<div class="middle">
<label for="_spring_security_remember_me" class="general-checkbox-label">
<input name="_spring_security_remember_me" tabindex="3" value="on" id="_spring_security_remember_me" class="checkbox margin-0px" type="checkbox">
<span>Keep me logged in </span>
</label>
</div>
<div class="last">
<button id="login_btn" type="submit" class="button-standard button-primary" tabindex="4">
<span class="button-standard-text">Login</span>
</button>
<a class="link" href="https://connect.data.com/forgotpassword" onclick="var x=".tl(";s_objectID="https://connect.data.com/forgotpassword_1";return this.s_oc?this.s_oc(e):true">Forgot Password?</a>
</div>
</div>
</div>
<div class="login-container marketing-message float-right">
<div class="content">
<h2>
Don't have an account?
Sign up now - it's FREE!
</h2>
<div class="float-left">
<img class="login-bicons" src="./Data.com Connect Business Contact Directory of Business Contacts and Company Information_files/clear.cache.gif">
</div>
<div class="float-left margin-left-20px">
<p>
<span id="pageTitle" style="color: #146791">Find</span>
business card information & B2B professionals.
</p>
<p>
<span id="pageTitle" style="color: #87c540">Research</span>
people & companies.
</p>
<p class="margin-top-5px">
<span id="pageTitle" style="color: #f37521">Get</span>
millions of contacts.
</p>
</div>
<div class="clear"></div>
</div>
</div>
<div class="clear nonlogged-container-foot">
<span class="new-to-ddc">New to Data.com?</span>
<a class="sign-up" href="https://connect.data.com/registration/signup" onclick="var x=".tl(";s_objectID="https://connect.data.com/registration/signup_1";return this.s_oc?this.s_oc(e):true">Sign up for an account...</a>
</div>
</div>
<input type="hidden" name="CSRF_TOKEN" id="CSRF_TOKEN" value="ce56932e08fc97bc29c5f6535b572664a66000513143584504b0ee7c66ef9659"></form>
i'm having a problem with the following code:
iFrame = EC.frame_to_be_available_and_switch_to_it(("MAIN_IFRAME"))
uscita = EC.presence_of_element_located((By.XPATH, "//input[contains(.,'password')]"))
uscita.send_keys('passwd')
and i'm getting the following error:
AttributeError: 'presence_of_element_located' object has no attribute 'send_keys'
i'm new Python's user and I would like your help on this problem.
Thanks
HTML for iframe and input:
<td style="text-align:center">
<iframe height="350" width="450" name="timb" src="timb.php" style="position: relative;top:0px"></iframe>
</td>
<td>
<div style="position: relative;top:0px">
<form action="mnghlog6.php" method="post" target="timbri">
<input type="hidden" id="esculappio" name="escu" value="0">
<table style="position: relative;top:0px">
</div></td><td><div class="buttons" style="display:inline;text-align: left;">
</div></td></tr><tr><td><div class="buttons" style="display:inline;text-align: left;">
</div></td><td><div class="buttons" style="display:inline;text-align: left;">
</div></td></tr></tbody></table> </div>
</td>
</tr>
<tr>
<td style="text-align:center">Password <input type="password" name="password" id="password" size="30" value=""></td>
</tr>
</tbody></table>
<input type="hidden" name="tipo" value="">
<input type="hidden" name="flag_inizio">
<input type="hidden" name="durata">
</form>
</div>
</td>
</tr>
You need to use until function from WebDriverWait with the expected_conditions. It also doesn't looks like the field is in iframe. Try this
wait = WebDriverWait(driver, 10);
uscita = wait.until(EC.presence_of_element_located((By.ID, "password")))
uscita.send_keys('passwd')
By the way, to switch to the frame you can do something like
iFrame = wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "timb")))
My form HTML source is below, I am trying to "check one of the checkboxes" and hit the "update" or submit button using mechanize. How would I go about this? Does the variable for linear_entitlements make it not possible?
<form accept-charset="UTF-8" action="/admin/users/3548003/user_linear_entitlements" class="form with-border" id="edit_user_3548003" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="samplevalue"></div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>GUID</th>
<th>CMS Subpack ID</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
<tr id="linear_entitlement_1">
<td>
<div class="control-group">
<label class="checkbox">
<span>SUN Pack</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="1">
</label>
</div>
</td>
<td> 2a59739c-13ed-11e2-a36b-12313d298802 </td>
<td> 1 </td>
<td> 2014-02-12 21:32:56 UTC <div style="float:right">→</div></td>
</tr>
<tr id="linear_entitlement_7">
<td>
<div class="control-group">
<label class="checkbox">
<span>Tamil Pack - Legacy</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="7">
</label>
</div>
</td>
<td> 2ab298dc-13ed-11e2-a36b-12313d298802 </td>
<td> 3 </td>
<td> 2015-04-01 23:11:33 UTC <div style="float:right">→</div></td>
</tr>
</tbody>
</table>
<div class="form-actions">
<input class="btn primary input_submit" name="commit" type="submit" value="Update"> <button type="reset" class="btn">Cancel</button>
</div>
</form>
So far I have this, which selects the form:
htmlResponse2 = browser.open(URL + 'admin/users/' + old_user_url + '/edit')
browser.select_form(nr=0)
I don't know if you need to select the form, but if you did so, following should just do the trick:
br.find_control(type="checkbox").items[0].selected=True
if you want to select all checkboxes:
for i in range(0, len(br.find_control(type="checkbox").items)):
br.find_control(type="checkbox").items[i].selected =True
then submit
br.submit()