Use getControl to control objects other than the name variable - python

I am using the Zope testbrowser which has been recommended in my last question. The problem that I am facing is that I can use the getControl function to control different objects like: password, username etc.
I am trying to submit the page to get to the next page but the submit button has no 'name' variable, just an 'id' variable. 'Submit' is written as follows:
<input type="submit" id="lgn_button" class="button" tabindex="3" accesskey="s" />
and the other objects are written as:
<input type="password" class="button" name="password" id="password" size="24" maxlength="20" accesskey="p" tabindex="2" value=""/></td>
I have no access to change this. The python zope code I am using to gain control of the 'password' object is:
browser.getControl(name='password')
The submit button doesn't have 'name' so I have written:
browser.getControl(id='lgn_button')
This prints out the error that 'id' is invalid:
TypeError: getControl() got an unexpected keyword argument 'id'
Is there any way to gain control of one of the other values in 'submit'.
Thanks for any help.

I assume that, for one reason or another, you can't add a 'name' attribute to your tag, but if it's only a 'name' that you can't add, you can explicitly set a 'value=Submit' (instead of relying on the default one, which is Submit) and then use browser.getControl('Submit')
Failing that, you can do something along the lines of
for form in browser.mech_browser.forms():
for control in form.controls:
if control.id == 'lgn_button':
return control
You might even want to extend Browser.getControl() with that and contribute it back to zope.testbrowser. ;)

Related

Python - How can I pass variable in Flask when clicked on a button

I am working on a Flask project that has two pages (firstpage, secondpage). The first page has two buttons. When a user clicks on one of them, it should send a variable (variable name is value) with text to the second page. The second page should display message depending on what button the user clicked in the first page. But my program is always printing the second value even when the first button is clicked. If I declared the variable global, can I use it in the secondpage?
My html code looks kind of like this:
<form action="/firstpage" method="post">
<div><h2 class="header">Click one button</h2></div>
<div class="pickitem">
<button class="btn one" name="btn-one" type="submit">ONE</button><br>
<button class="btn two" name="btn-two" type="submit">TWO</button></div>
</form>
and my Python code looks like this:
var value=""
#app.route("/firstpage", methods=["GET", "POST"])
def mymethod():
value = ""
if request.method == "POST":
if request.form.get("btn-one"):
value = "uno"
else:
request.form.get("btn-two"):
value = "dos"
print (value)
return render_template("secondpage.html")
else:
return render_template("firstpage.html")
You can use an hidden input field for this.
HTML:
<form action="/firstpage" method="post">
<div><h2 class="header">Click one button</h2></div>
<input type="hidden" name="btn-pressed" class="hidden-field">
<div class="pickitem">
<button class="btn one" name="btn-one" type="submit">ONE</button><br>
<button class="btn two" name="btn-two" type="submit">TWO</button></div>
</form>
Change its value depending upon the button clicked.
JavaScript:
document.addEventListener('click', (e) => {
// do not process if the clicked element is not a form button
if ( ! e.target.matches('form .btn') ) return;
// prevent form submission
e.preventDefault();
// change hidden field value
let field = document.querySelector('.hidden-field');
field.value = e.target.getAttribute('name');
// submit the form
e.target.closest('form').submit();
});
Then in Python, you can get the value through the name btn-pressed
Python:
request.form.get("btn-pressed") # to get the hidden field value
A few syntax errors you might need to fix.
It seems like you are mixing up different programming languages. In the first line, you entered var value="". Try to remove the var.
Make sure your indentation is correct! Indent the block from def mymethod(): to the bottom.
Remove the colon from the line request.form.get("btn-two"):. Did you mean if request.form.get("btn-two"): instead?
Apart from that, the reason value ends up not being either "uno" or "dos" is because of the if-statements in the code. To fix the issue, change them to:
if list(request.form)[0] == 'btn-one':
Why does this work? The request.form looks like this:
ImmutableMultiDict(['btn-one', ''])
Transforming this into a list (i.e., getting its keys, which is either btn-one or btn-two) and taking the first item will tell us the button pressed.
Hope this helps :)

Input text field value behavior after Get request

I have this form
<form class="form-inline" type="get" action=".">
<input id="excel_input" class="form-control" type="text" name="excel_input" value="0">
<input class="btn" type="submit" name="excelbutton" value="excelbuttonclicked"
onclick='document.getElementById("excel_input").value = "1";'><i class="ion ion-search"></i></input>
</form>
I want to get the value "1" when the excelbutton clicked in the Get request in order to export an excel file. I want to use the Get request because in this point I have a ready query set , the query set user sees on the screen.
I use the commands
request.GET.get('excel_input', None)
if excel_string not in ['0', None]:
testxlsxwriter(d_list)
to get the value of the input field and export the excel file. The problem is that I get the value "1" from the input field on button click , but then I always get the value "1" on get requests( refresh , next page etc.
I see the value "0" on screen on refresh but I get the value "1" on my view
I also tried javascript with no luck
window.onload = function(){
document.getElementById("excel_input").value = "0";
}
Can someone help me please to understand how get request works with this input element?
Thanks a lot
Kostas

Verify input box text using Python + Selenium

I have gone through other similar questions and the general advice was to use the get_attribute() method with input 'value'.
However, this doesn't seem to work in my case.
The tag I am trying to test is -:
<input class="form-control required" id="email" name="email" placeholder="Enter your E-mail / Username" required="" type="text" value="">
and the code I am using is as follows -:
def enter_keys_by_id(self, id, text):
"""
Finds a form element by ID and then proceeds
to enter text into the form.
"""
find_element = self.driver.find_element_by_id(id)
assert find_element.get_attribute('value') == ""
find_element.send_keys(text)
assert find_element.get_attribute('value') == text
return find_element
with the function call -:
enter_keys_by_id("email", "someemail#email.com")
The 'value' attribute is set to blank in the tag. How would I go about it in this particular case?
So, you type email to the "empty" input, but I think that you need to submit the form, ie save the value to the input before retrieveing it. I guess you are trying to verify that the email was saved correctly, so you can:
Type email
Submit the form (you didn't provide html, but I guess there is some submit button near to the email field)
And after that try to assert find_element.get_attribute('value') == text (or to wait for the input with value='your text')

Selecting an unnamed text field in a mechanize form (python)

So i'm making a program to batch convert street addresses to gps co-ordinates using mechanize and python. this is my first time using mechanize. I can select the form ("form2') on the page. however the text box in the form has no name. how do i select the textbox so that mechanize can enter my text? I've tried selecting it by its id. but that does not work.
br.select_form("Form2") #works as far as i know
br.form["search"] = ["1 lakewood drive, christchurch"] #this is the field that i cannot select
and here is the source code from the website.
<form name="Form2" >
or Type an <b>Address</b>
<input id="search" size="40" type="text" value="" >
<input type="button" onClick="EnteredAddress();" value="Enter" />
</form>
any help would be much appreciated.
form.find_control(id="search") ?
FWIW I solved this by using the above answer by lazy1 except that I was trying to assign a value after using the find_control method. That didn't work of course because of assignment, I looked deeper into the method and found setattr() and that worked great for assigning a value to to the field.
will not work
br.form.find_control(id="field id here") = "new value here"
will work
br.form.find_control(id="field id here").__setattr__("value", "new value here")

Always the same results through ZCatalog? Everything is set by the book!

It is strange, but I can't get the ZCatalog working. Everything is done by the book and still the results are always the same full list.
You can see here:
http://nfp-bg.eionet.eu.int/waste/en/search-results?catalogTextIdx=selection
What I have done so far:
Created ZCatalog Object
Created ZCTextIndex Lexicon with the following items
HTMLWordSplitter
CaseNormalizer
StopWordRemover
Created catalogTextIdx Index - the parameters are:
Name(s) of attribute(s) indexed: PrincipiaSearchSource
Index type: Okapi BM25 Rank
ZCTextIndex Lexicon used: http://nfp-bg.eionet.eu.int/waste/catalog//catalogTextIdx/catalogLexicon
The MetaData collected is:
PrincipiaSearchSource
id
title
The Find Object is run only for Objects of Type: DTML Document
The list of items is created correctly for the catalog (I have removed only the css / js dtml files)
The following code is used for the Search form:
<form action="search-results" method="get">
<input type="text" name="catalogTextIdx" id="catalogTextIdx" value="<dtml-if catalogTextIdx>
<dtml-var catalogTextIdx><dtml-else>Search...</dtml-if>" class="search-field" />
<input type="submit" name="SUBMIT" value="Submit Query" class="button" />
</form>
Finally the following code is used for the Search Results Page:
<dtml-with common>&dtml.-Header;&dtml.-left-column;</dtml-with>
<td id="content" valign="top">
<h2>Search Results</h2>
<dtml-in expr="catalog(meta_type=['DTML Document'])">
<h3>Result founded: "><dtml-var title></h3>
<dtml-var "filterRenderedHTML(PrincipiaSearchSource)">
</dtml-in>
</td>
<dtml-with common>&dtml.-right-column;&dtml.-Footer;</dtml-with>
It should be working, but it's not. I believe it should be something small, but still not sure.
Thanks in advance for all your help.
The ZCatalog has a quirk which is that if your query includes an index that does not exist in the catalog, it will return all indexed objects. In this case, your query appears to involve a "meta_type" index which you did not set up. So the ZCatalog tries to apply that index, but doesn't find it, so it returns all the items.

Categories

Resources