304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Close Android Chrome Tabs – Now With Automation

I wanted a way to close Android Chrome tabs, and now I have it.

Close Android Chrome Tabs – Introduction

First of all, I wanted to wish everyone a Belated Happy Valentine’s day!

If you read my last post about interacting with Android Chrome tabs, then you have an idea of where this one may be going.

First, I wanted to note that the code on that post was out-of-date, and I’ve provided the updates below (and updated the original post).

tabs = Array.from(document.querySelector('div /deep/ div /deep/ div /deep/ div /deep/ div /deep/ div /deep/ div.vbox.flex-auto').shadowRoot.querySelectorAll('.devices-view .device-page-list .vbox'), s => ({name: s.querySelector('.device-page-title').textContent, url: s.querySelector('.device-page-url .devtools-link').getAttribute('href')}))
str = '';
for (i = 0; i < tabs.length; i++){
    if (tabs[i].name != null){
      str += '- [' + tabs[i].name + '](' + tabs[i].url + ')\n'
    } else {

That said, I received a great comment asking how to close these tabs as well.

After a lot of research and testing, I was finally able to figure this out. I’ll walk through the manual process, as well as provide a short script to automate it at the end.


First, you will need ADB installed on your local system.

Once you have ADB installed and running, you can verify the connection after enabling USB debugging.

C:\Users\Ray>adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached
70xxxxxxxxxxxx  unauthorized

Next, create a local port forwarding rule from a port to the chrome_devtools_remote on the target device.

C:\Users\Ray>adb forward tcp:9222 localabstract:chrome_devtools_remote

Close Android Chrome Tabs – Research and Closing Tabs

During some of my searches, I came across this post that mentioned the port forwarding and the /json Chrome endpoints.

First, I went to http://localhost:9222/json/version to ensure that my forwarding worked.

Close Android Chrome Tabs - Version

With that working, I also went to the http://localhost:9222/json/list endpoint, to get a list of my tabs.

Close Android Chrome Tabs - List

Note that this is also another way to extract every tab from the device, as opposed to the DevTools and JavaScript method.

Closing the Tabs

The JSON endpoint was something that I was hoping to use to close my tabs, but couldn’t get it working initially. For more information, I recommend the following posts that I spent some time on.

First, I picked one of my tabs that I wanted to close, and wrote down the id.

[ {
   "description": "",
   "devtoolsFrontendUrl": "",
   "id": "E49927F7E60C49875AF1E0A783ACD140",
   "title": "",
   "type": "other",
   "url": "",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/E49927F7E60C49875AF1E0A783ACD140"
}, {
   "description": "",
   "devtoolsFrontendUrl": "",
   "id": "16025",
   "title": "Should You Do Your Own Taxes? (and Why I Don't) | Mr. Money Mustache",
   "type": "page",
   "url": "",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/16025"
}, {

Next, I went to http://localhost:9222/json/close/16025, and received the message, “Target is closing”. When I went back to my Android device, I saw that the tab was closed!


With manual closing verified, it was time to write a script to help Matt, and anyone else out.

You can find the code below, but it is obviously pretty simple.

import json
import requests

response = requests.get("http://localhost:9222/json/list")


json_data = json.loads(response.text.encode('utf-8'))

for link in json_data:
    response = requests.get("http://localhost:9222/json/close/" + link['id'])

When I ran this script, it closed all 80 tabs that I had open quickly.

Target is closing
Target is closing
Target is closing
Target is closing

As you can see, when I went back to my device, I had zero open tabs.

Close Android Chrome Tabs - Zero tabs

Finally, you can find the code and any updates in my GitHub repository

Close Android Chrome Tabs – Conclusion

This was a simple solution and script, but I’m glad that I finally got to answer the comment on my original post.

Let me know if you have any other useful tricks for people who abuse tabs like myself!

Con season is coming up, so hopefully I will have more CTF or general conference posts. I’m also still working through the eLearnSecurity XDS course, and I’m hoping to finish that up before Q2.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.