# Fix "'xml.etree.ElementTree.Element' object has no attribute 'getchildren'" in Python 3.9+

Yes. An agent replaces Element.getchildren() and getiterator() with iteration, guards missing nodes correctly, and compares the output across versions.

[Run this on Rush](https://cloud.getrush.ai/run/fix-elementtree-getchildren-attribute-error/elementtree/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

`Element.getchildren()` and `getiterator()` were long deprecated and then removed from `xml.etree.ElementTree` in Python 3.9. The scripts that still call them are often glue code nobody runs by hand, which is how the failure goes unnoticed. The C# example in gradescope/autograder_samples has one. `c#/src/run_autograder` compiles a student's submission with `mcs`, runs `nunit-console`, and ends with `python3 nunit_to_gs.py > /autograder/results/results.json`. The converter, `nunit_to_gs.py`, calls `.getchildren()` on lines 13, 112 and 116. On Python 3.9 or later it dies on line 112 with the error in the title, and because the traceback goes to stderr while stdout is redirected, the results file is left empty.

The agent needs an input to reproduce it, and the directory has none: there is no saved `TestResult.xml`. It reads the converter to learn the shape it expects (a `test-suite` under the root with a `time` attribute, nested `results` elements, and `test-case` nodes with `properties`, `success` and an optional `failure` message) and builds a sample that mirrors the two tests in the README, one visible with weight 1 and one hidden with weight 2 and the name `Bye`. On Python 3.8 the script converts that file and prints `DeprecationWarning: This method will be removed in future versions. Use 'list(elem)' or iteration over elem instead`. The agent saves the JSON as the baseline.

The fix follows the warning's advice. An `Element` is iterable over its direct children, so `for child in node.find('results').getchildren():` becomes `for child in node.find('results'):`, with `list(elem)` where the code needs a real list to index or measure. All three calls here are loops, so three method calls disappear and nothing else changes. `getiterator()`, where a project uses it, becomes `iter()`, which walks the whole subtree and not only the children. The agent runs the converter on 3.8 and on current versions and compares each result with the baseline.

It then looks at what surrounds those lines, because ElementTree has a second trap right next to the first. `find()` returns `None` when the child is missing, and a test case with no `properties` element crashes the converter both before and after the change, only with a different message. The tempting guard, `if node:`, is wrong: an element with no children is falsy, and Python 3.12 warns that testing an element's truth value is deprecated. The agent adds `is not None` checks where the prompt allows and covers a property-less test case in its sample.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails on Python 3.9 or later with \"AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'\". Find every call to getchildren() and getiterator() on ElementTree elements or trees in the repository, and check whether each object really comes from xml.etree and not from lxml, which still provides getchildren. Find how the affected script is invoked (a shell script, a CI step, a Dockerfile) and what happens to its output when it crashes. Capture a baseline before changing anything: run the script on Python 3.8 against a sample input and save the output. If the repository has no sample input, write a small one from what the code reads, and say that it is hand-written. Replace getchildren() with direct iteration over the element, or list(elem) where a list is needed, and getiterator() with iter(). Then check the code around each call for a find() result used without a None check, and for truth tests on elements such as 'if node:'; replace those with 'is not None' or len() and add a sample case that exercises a missing child. Do not change the output format. Run the script on 3.8 and on a current Python and show that the output matches the baseline. If the invoking script discards errors, report that as a follow-up. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that edits one Python file in a handful of places and adds a sample input with its expected JSON. For the Gradescope converter the core diff is three shortened `for` lines. The description shows the deprecation warning from 3.8, the traceback from a current Python, and the comparison: the same sample converted on each interpreter, with identical output.

The description is explicit about what the evidence is and is not. The sample XML was written by the agent from the fields the converter reads, and the pull request says so. It does not claim that Mono and NUnit were installed and a real submission graded, unless the agent got `setup.sh` to complete and ran the pipeline end to end, in which case the generated result file replaces the hand-written one.

Two follow-ups are listed and left for the reviewer. One is the invoking script: `run_autograder` redirects stdout into the results file and has no check on the converter's exit status, so any future crash again produces an empty file. The other is each `find()` call the agent guarded and what the converter now emits for a test case with no properties. The reviewer confirms that behaviour is what the course staff want, a default score or a clear error.

## Run it on every new GitHub issue

Glue scripts fail far from the people who can fix them, and the report usually arrives as an issue with a pasted traceback. Add an automation on the repository's Project with the prompt above and a `github` trigger on `issues.opened`, with `labels` set if you only want triaged issues to count. The issue is added after the prompt as event text, so the traceback is in front of the agent as data. Only issues from a repository owner, member or collaborator fire the trigger.

If you would rather catch it before anyone reports it, add a `schedule` trigger to the same automation with a monthly `cron`. The prompt already tells the agent to search for the removed methods, so a scheduled run on a repository that is clean ends with a short answer and no pull request.

## Why run it on Rush?

The script in question runs inside someone else's pipeline, on whatever Python that image ships, with its errors redirected away. Nobody has the old interpreter and the new one side by side on a laptop, plus a sample input, plus the patience to diff two JSON files for a three-line change. The agent's sandbox is a fine place for exactly that: it installs both interpreters, generates the sample, runs the comparison and keeps the outputs with the session.

It also reads past the line in the traceback. The method removal is what broke, but the missing `None` checks and the silent redirect are why a small break can turn into an empty results file for every submission. A run that returns the fix together with those two findings gives the maintainer the whole picture in one pull request, with each claim backed by a command they can rerun.

## Frequently asked questions

### What replaces getchildren()?

Iterating the element directly: `for child in elem:`. Use `list(elem)` when you need to index, slice or count the children, and `len(elem)` for the count alone. These have worked on every Python 3 version, so the change does not raise your minimum version.

### And getiterator()?

`elem.iter()` or `elem.iter('tag')`. It visits the element itself and every descendant in document order, the same traversal `getiterator()` performed. It is not a substitute for `getchildren()`, which returned direct children only.

### My code uses lxml. Is it affected?

No. lxml's elements still have `getchildren()`, though its own documentation prefers iteration. The error in the title names `xml.etree.ElementTree.Element`, the standard-library class. The agent checks which library produced each element before editing a call.

### Why not write if node: to check that an element exists?

Because an element's truth value reflects whether it has children, not whether it was found. A present but empty element is falsy, so the check skips valid data. Python 3.12 prints a deprecation warning for it and recommends `elem is not None` or `len(elem)`.

## More jobs like this: Fix a bug from an alert

- [Fix "TypeError: db.collection(...).ensureIndex is not a function" after a MongoDB driver upgrade](https://getrush.ai/run/fix-ensureindex-is-not-a-function/mongodb/)
- [Fix ERR_PNPM_OUTDATED_LOCKFILE When CI Runs pnpm install](https://getrush.ai/run/fix-err-pnpm-outdated-lockfile/pnpm/)
- [Fix "ESLint couldn't find an eslint.config.* file" after upgrading to ESLint 9](https://getrush.ai/run/fix-eslint-couldnt-find-eslint-config-file/eslint/)
- [Fix "TrainingArguments.__init__() got an unexpected keyword argument 'evaluation_strategy'"](https://getrush.ai/run/fix-evaluation-strategy-unexpected-keyword-argument/transformers/)
- [Fix "expected exactly 5 fields, found 6" After Upgrading robfig/cron to v3](https://getrush.ai/run/fix-expected-exactly-5-fields-found-6/robfig-cron/)
- [Fix "error: externally-managed-environment" in a Docker Build](https://getrush.ai/run/fix-externally-managed-environment/docker/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
