I use the Mechanize for filling the form of filtering.
My code:
br = Browser()
br.open(self.domain)
br.select_form(nr=1)
br.find_control("pf_keywords").value = "Lisp"
response = br.click(type='button', nr=0)
#or
response = br.submit(label='Применить фильтр')
At the same time, submit button is not in the list of controls for this form.
Html code for this button:
<button type="button" class="b-button b-button_flat b-button_flat_green" onclick="$('frm').submit();">Применить фильтр</button>
Because of this, using the method click() and submit() impossible to produce form submission. In these methods, there is a search of the desired control with the parameters passed to the control environment forms, as desired button is not there, raise a bug :
mechanize._form.ControlNotFoundError: no control matching type 'button', kind 'clickable'
What should I do? How can push a button and get the result?
Related
I created a program to fill out an HTML webpage form in Selenium, but now I want to change it to requests. However, I've come across a bit of a roadblock. I'm new to requests, and I'm not sure how to emulate a request as if a button had been pressed on the original website. Here's what I have so far -
import requests
import random
emailRandom = ''
for i in range(6):
add = random.randint(1,10)
emailRandom += str(add)
payload = {
'email':emailRandom+'#redacted',
'state_id':'34',
'tnc-optin':'on',
}
r= requests.get('redacted.com', data=payload)
The button I'm trying to "click" on the webpage looks like this -
<div class="button-container">
<input type="hidden" name="recaptcha" id="recaptcha">
<button type="submit" class="button red large">ENTER NOW</button>
</div>
What is the default/"clicked" value for this button? Will I be able to use it to submit the form using my requests code?
Using selenium and using requests are 2 different things, selenium uses your browser to submit the form via the html rendered UI, Python requests just submits the data from your python code without the html UI, it does not involve "clicking" the submit button.
The "submit" button in this case just merely triggers the browser to POST the form values.
However your backend will validate against the "recaptcha" token, so you will need to work around that.
Recommend u fiddling requests.
https://www.telerik.com/fiddler
And them recreating them.
James`s answer using selenium is slower than this.
Using BeautifulSoup on Python, I'm trying to scrape a subpage of this page
https://www.mmorpg-stat.eu/0_fiche_alliance.php?pays=5&ftr=500208.all&univers=_146
More precisely, the subpage titled
The problem is that by clicking on that button, the url doesn't change (is this called a subpage? If not what is it?) so I cannot access that page with
url = '...'
requests.get(url)
Looking at the browser console, the button code is
<td width="250" align="center" valign="middle" class="Style1_f_j barre_joueur1 fond_56_1" style="cursor:pointer;text-transform: uppercase" onclick="fcache12('faCacher');fcache13('ffond_gris');document.form1_2date.statview.value='2';document.forms['form1_2date'].submit();return false;">
<span style="color:#ffffff;"> Other information</span>
</td>
All I can understand is that when clicked, the button calls some fcache method.
How to access the subpage?
All I can understand is that when clicked, the button calls some fcache method.
onclick="fcache12('faCacher');fcache13('ffond_gris');document.form1_2date.statview.value='2';document.forms['form1_2date'].submit();return false;"
It actually calls two different methods: fcache12() and fcache13(). And then it finds a form in the page and submits it:
document.forms['form1_2date'].submit()
If you search 'form1_2date', you will find:
<form name="form1_2date" method="post">
So to simulate clicking on this button, you need to call requests.post() instead of requests.get(). You also need to determine the form values that should be passed in. These are determined by all of the <input> tags in the form.
Alternatively, you can use selenium or a similar library to simulate user interaction in a browser rather than trying to make the requests directly.
I'm trying to write a Python code to submit a simple form.
http://stulish.com/soumalya01
[Edit : http://travelangkawi.com/soumalya01/
When you use this link it returns a different page on form submit. Is good for debugging]
Any code would do
Tried both mechanize and mechanical soup. Both are unable to handle the text fields. It does not have a name only ID. But we are unable to get the element by ID
Any Code would do as long as it works. (Fill ABC in the text box and hit submit)
I just followed the documentations of mechanize. See sample code below:
from mechanize import Browser
br = Browser()
br.open('http://stulish.com/soumalya01')
br.select_form(nr=0)
form.set_all_readonly(False) #add this
br.form.set_value('ABC', nr=1)
print(br.form.controls[1])
br.submit()
I'm trying to submit a form on an .asp page but Mechanize does not recognize the name of the control. The form code is:
<form id="form1" name="frmSearchQuick" method="post">
....
<input type="button" name="btSearchTop" value="SEARCH" class="buttonctl" onClick="uf_Browse('dledir_search_quick.asp');" >
My code is as follows:
br = mechanize.Browser()
br.open(BASE_URL)
br.select_form(name='frmSearchQuick')
resp = br.click(name='btSearchTop')
I've also tried the last line as:
resp = br.submit(name='btSearchTop')
The error I get is:
raise ControlNotFoundError("no control matching "+description) ControlNotFoundError: no control matching name 'btSearchTop', kind 'clickable'
If I print br I get this: IgnoreControl(btSearchTop=)
But I don't see that anywhere in the HTML.
Any advice on how to submit this form?
The button doesn't submit the form - it calls some javascript function.
Mechanize can't run javascript, so you can't use it to click that button.
The easy way out is to read that function yourself, and see what it does - if it just submits the form, then maybe you can get around it by submitting the form without clicking on anything.
you need to inspect element first, did mechanize recognize the form ?
for form in br.forms():
print form
check the following script:
from mechanize import Browser
br = Browser()
page = br.open('http://scottishladiespool.com/register.php')
br.select_form(nr = 5)
r = br.click(type = "submit", nr = 0)
print r.data
#prints username=&password1=&password2=&email=&user_hide_email=1&captcha_code=&user_msn=&user_yahoo=&user_web=&user_location=&user_month=&user_day=&user_year=&user_sig=
that is, it doesn't add the name=value pair of the submit button (register=Register). Why is this happening? ClientForm is working properly on other pages, but on this one it is not. I've tried setting the disabled and readonly attributes of submit control to True, but it didn't solve the problem.
There is a disabled=disabled attribute on the register button. This prevents the user from clicking and presumably mechanize respects the disabled attribute as well.
You'll need to change the source code of that button. Enabling the control means completely removing the disabled=disabled text.