2

I am having trouble modifying the value of a check box using the Python library Mechanize. The problem is that the form will not perform the desired action due to the fact that the value is modified by JavaScript, which Mechanize does not support.

The find_control() method returns an error. Below is the problematic snippet of code:

from mechanize import Browser
stringGoesHere = "stringGoesHere"
br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open("http://example.com/form.php")
br.select_form(nr=0)
br.find_control(name="pkid").value = ["stringGoesHere"]
br.submit()

The error returned is: "mechanize._form.ItemNotFoundError: insufficient items with name 'stringGoesHere'". I have also tried replacing the find_control() method with the below line of code with the same resulting error message:

br['pkid'] = [stringGoesHere]

If the input is ignored, I have been able to create a new form field named "pkid" with the value needed, but have not tested this solution. It also seems a little backwards to me that an additional input with the same name would have to be created to get the desired effect:

br.form.new_control('text','pkid',{'value':'stringGoesHere'})
br.form.fixup()

I was hoping someone could point me in the right direction on this. The docs for Python Mechanize is a little lacking, and a Google search didn't really help either.

A stripped down version of the form I am trying to submit is below:

<form name="formName" method="post" action="http://example.com">

         <input type="checkbox" id="0" onclick="uncheckMasterCheckbox(); enableExtension('0', 'telephonenumber_0')" class="content-nogroove" name="pkid" value='wglkweg-ewrg-44e4-b2c1-wegwergb5:'/>

         <input type="text" id="telephonenumber_0" name="telephonenumber" value="" disabled size="10" onchange="var e = document.getElementById('0'); e.value = 'wglkweg-ewrg-44e4-b2c1-wegwergb5:' + document.getElementById('telephonenumber_0').value;" onblur="var e = document.getElementById('0'); e.value = 'wglkweg-ewrg-44e4-b2c1-wegwergb5:' + document.getElementById('telephonenumber_0').value;" onkeyup="numbersOnlyIncludeZero('telephonenumber_0')"/>

         <button class="content-formbutton" id="importUsersButton" onclick="save();return false;">Import Selected</button>

    </form>

0 Answers0