Selecting an unnamed text field in a mechanize form (python) - 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")

Related

How to set a text in a textarea by using Mechanical Soup?

I'm learning to create an Omegle bot, but the Omegle interface was created in HTML and I don't know very much about HTML nor MechanicalSoup.
In the part where the text is inserted, the code snippet is as follows:
<td class="chatmsgcell">
<div class="chatmsgwrapper">
<textarea class="chatmsg " cols="80" rows="3"></textarea>
</div>
</td>
In the part of the button to send the text, the code snippet is:
<td class="sendbthcell">
<div class="sendbtnwrapper">
<button class="sendbtn">Send<div class="btnkbshortcut">Enter</div></button>
</div>
</td>
I want to set a text in textarea and send it via button.
Looking at some examples in HTML, I guess the correct way to set text in a textarea is as follows:
<textarea>Here's a text.</textarea>
Also, I'm new at MechanicalSoup, but I think I know how to find and set a value in an HTML code:
# example in the Twitter interface
login_form = login_page.soup.find("form", {"class": "signin"})
LOGIN = "yourlogin"
login_form.find("input", {"name": "session[username_or_email]"})["value"] = LOGIN
From what I understand, the first argument is the name of the tag and a second argument is a dictionary whose first element is the name of the attribute and the second element is the value of the attribute.
But the tag textarea don't have an attribute for setting a text, like value="Here's a text.". What I should do for set a text in a textarea using MechanicalSoup?
I know it's not the answer you expect, but reading the doc would help ;-).
The full documentation is available at:
https://mechanicalsoup.readthedocs.io/
You probably want to start with the tutorial:
https://mechanicalsoup.readthedocs.io/en/stable/tutorial.html
In short, you need to select the form you want to fill-in:
browser.select_form('form[action="/post"]')
Then, filling-in fields is as simple as
browser["custname"] = "Me"
browser["custtel"] = "00 00 0001"
browser["custemail"] = "nobody#example.com"
browser["comments"] = "This pizza looks really good :-)"

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

selenium not setting input field value

Let's say we have this website https://www.coinichiwa.com/ which has a BET AMOUNT input box. It's html is:
<input autocomplete="off" id="betFa" name="a" maxlength="20" value="0.00000000" class="betinput" style="">
I need to add some value into it. Here is my code:
browser = webdriver.Firefox()
browser.get('https://www.coinichiwa.com')
browser.find_element_by_id("betFa").send_keys("0.00000005")
print browser.find_element_by_xpath("//input[contains(#id,'betFa')]").text
But it's neither setting it's value to "0.00000005" nor it printing the value of input.
I'm not sure what's going wrong. Can you suggest?
Why it's not working?
You need to clear() the text input first:
bet_fa = browser.find_element_by_id("betFa")
bet_fa.clear()
bet_fa.send_keys("0.00000005")
As for the your second problem - this is an input and the value you enter into it is kept inside the value attribute, not the text. Use get_attribute() method:
browser.find_element_by_xpath("//input[contains(#id,'betFa')]").get_attribute('value')

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.

Use getControl to control objects other than the name variable

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. ;)

Categories

Resources