You should be able to follow the wire up to the engine bay. You may have to remove the air intake snorkel and engine cover. You will see it. Bright colored wire sheath. Removal will be from below or from passenger side wheel well with tire removed.
Firstly I am surprised that almost all the info here is for first and second-gen Foresters. That said in the quest to engage in some preventative maintenance, I tried to remove my upstream O2 sensor ( technically air-fuel sensor), and the mother will not budge, and my O2 socket is rounding it...
read my thread above. I put in pics of the location. You can access the connector through the front passenger side wheel well after you have disconnected the plastic under cladding on that side. The oxygen sensor has a yellow wire that goes in a black connector, the air-fuel sensor (the upstream) has a bright green wire and a light gray connector.