Getting "AttributeError: 'str' object has no attribute 'append'" with mechanicalsoup's submit function

541 Views Asked by At

I am new to python and I'm attempting to use it to automate a manual form-filling task at work.

I am utilizing mechanicalsoup functions to navigate a website and fill out the same form using values from a csv file. I can confirm the code below works for the first row of values from my csv, but breaks after that. My csv file has three columns.

I would really appreciate any help with this. Thank you.

Here is my code:

#login
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("<some website url>")
browser.select_form('form[action="/logins"]')
browser["username"] = "***"
browser["password"] = "*****"
browser.submit_selected()

#navigate to form and fill
def myfunction(esp, supnum, mid):
    url_esp = "<some website url>" + esp
    browser.open(url_esp)
    browser.follow_link("<some link>")
    url_facility = browser.get_url()
    url_facility_edit = url_facility + "<some link>"
    browser.open(url_facility_edit)
    browser.select_form('form[id="edit_facility"]')
    browser["facility[oracle_identifier]"] = supnum
    browser["facility[ffc_identifier]"] = mid
    response = browser.submit_selected()

#import values from csv to pass into function
import csv
with open('biweekly_oracle_esp_test.csv', 'r') as csv_file:
    reader = csv.reader(csv_file)
    for row in reader:
        esp = row[0]
        supnum = row[1]
        mid = row[2]
        myfunction(esp, supnum, mid)

Here is the error:

Traceback (most recent call last):
  File "open_file_test2.py", line 32, in <module>
    myfunction(esp, supnum, mid)
  File "open_file_test2.py", line 19, in myfunction
    response = browser.submit_selected()
  File "C:\Python\lib\site-packages\mechanicalsoup\stateful_browser.py", line 232, in submit_selected
    *args, **kwargs)
  File "C:\Python\lib\site-packages\mechanicalsoup\browser.py", line 213, in submit
    response = self._request(form, url, **kwargs)
  File "C:\Python\lib\site-packages\mechanicalsoup\browser.py", line 155, in _request
    data.setdefault(name, []).append(value)
AttributeError: 'str' object has no attribute 'append'
0

There are 0 best solutions below