How can you make a selection in a global_form ListControl element using mechanize (python)?

114 views Asked by At

I am working with a food bank in order to help them automatically create an entry for a new guest whenever someone fills out their intake form. In order to create a new guest, the outreach must first be selected. This is done through a ListControl that is part of the global form.

import mechanize
import http.cookiejar

br = mechanize.Browser()
cj = http.cookiejar.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]


# login

br.open('https://companywebsite.com/login')
br.select_form(nr = 0)
br.form['username'] = 'my_username'
br.form['password'] = 'my_password'
br.submit()


# Deselect the default placeholder value and select the name of the outreach

br.open('https://companywebsite.com/create-new-visit')
br.global_form().find_control(id='outreachSelect').items[0].selected = False
br.global_form().find_control(id='outreachSelect').items[1].selected = True
print(br.global_form().find_control(id='outreachSelect').items)

This is the output that I get. As you can see, the first item is still selected.

[<Item name='0' id=None value='0' selected='selected' contents='Select an Outreach' label='Select an Outreach'>, 
<Item name='558' id=None value='558' contents='Company Name' label='Company Name'>]

This screenshot shows how the website looks

This screenshot shows how the website looks

In order for the "Create New Guest" button to work, the "Select an Outreach" dropdown must be changed

1

There are 1 answers

0
Ameen On BEST ANSWER

After looking at it again, I found that the site was running javascript based on the contents of that dropdown. When the correct option was selected it would make a request to https://companywebsite.com/create-new-visit/set-outreach/?id=config_1.

I was able to solve my problem by making that same request using mechanize and then using the "Create New Guest" button.