/Selenium-On-Termux-Android

Tutorial about how to install and use selenium on termux android.

Why archive?

  • Testing and finding solutions is really difficult, so I won't continue this project.
  • You can wait for me to release something similar for android at Moe-Moe.
  • I dont have time to continue that project, also i renamed it to AwChromium.
  • Also thank you all :D, who visted my tutorial, and watch my video, its really cool !!!
  • Ai không biết tiếng anh thì sài google dịch đi !

Selenium-On-Termux-Android

  • This tutorial will help you how to install and use Selenium on Termux for Android.

Video

Tutorial video

Note

  • PLease give me star if you like this tutorial <3.
  • If you receive this error OSError: [Errno 8] Exec format error: '/data/data/com.termux/files/usr/lib/pythonX.YY/site-packages/selenium/webdriver/common/linux/selenium-manager'.
  • Where X.YY is your Python version, example 3.8, 3.9, 3.10, 3.11, 3.12, ...
  • That means you haven't installed the required package.

Download

Requirements

Step Command
1 Open Termux
2 Allow access to storage memory
3
termux-setup-storage
4 Force exit Termux
5 Reopen Termux
6 Update & Upgrade package
7
yes | pkg update -y && yes | pkg upgrade -y
8 Install pip (they seperated it from python)
9
yes | pkg install python-pip -y
10 Install selenium
11
pip install selenium==4.9.1
12 PLEASE MAKE SURE YOUR SELENIUM VERSION <= 4.9.1

Choose WebDriver you want install

Chromium Firefox
yes | pkg install x11-repo -y
yes | pkg install tur-repo -y
yes | pkg install chromium -y
yes | pkg install x11-repo -y
yes | pkg install firefox -y
yes | pkg install geckodriver -y

VNCServer ( Optional )

Step Command
1
2
vncserver -listen tcp
3 For first time you will see it show something like New 'localhost:1 ()' desktop is localhost:1, then localhost:1 is your display ip address
4 You can download VNC Viewer from CH Play to view your webdriver, use localhost:1 as the ip address

Example

Type Video Chromium Firefox
Headless Unknown
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com")
driver.save_screenshot("/sdcard/download/screenshot.png")
print("Please check screenshot image")
driver.quit()
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("https://www.google.com")
driver.save_screenshot("/sdcard/download/screenshot.png")
driver.quit()
Non-Headless
example.mp4
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--display=:1") # localhost:1 -> display ID = 1
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com")
driver.save_screenshot("/sdcard/download/screenshot.png")
driver.quit()
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument("--display=:1") # localhost:1 -> display ID = 1
driver = webdriver.Firefox(options=options)
driver.get("https://www.google.com")
driver.save_screenshot("/sdcard/download/screenshot.png")
driver.quit()

Chromium on Android (adb required)

Chrome Firefox
chrome.mp4
firefox.mp4
  • This is why you should use chromium instead of chrome and firefox
    • All data will be ERASED.
    • Bug/crash/error....

Install Android SDK

  • You need Android SDK to use adb control.

    yes | pkg install wget -y
    cd $HOME
    wget https://github.com/Lzhiyong/termux-ndk/releases/download/android-sdk/android-sdk-aarch64.zip
    unzip android-sdk-aarch64.zip -d android-sdk
    rm -r android-sdk-aarch64.zip
    echo "export ANDROID_HOME=$HOME/android-sdk" >> $HOME/.bashrc
    echo "export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools" >> $HOME/.bashrc
    

Requirements

  • You MUST install this to make it work.

    yes | pkg install android-tools -y
    yes | pkg install x11-repo -y
    yes | pkg install tur-repo -y
    yes | pkg install chromium -y
    

Enable ADB Server

  • You need ADB Server for selenium.

    For rooted device
    • This is the easiest option.

      su -c stop adbd && su -c start adbd
      su -c setprop service.adb.tcp.port 5555
      
    For Android >= 11 and above
    • you can watch Video tutorial for how to do that.
    • Run adb pair localhost:<port_1> <security_code>.
    • <port_1> != <port_2>, same port but it changes when pairing.
    • Run adb connect localhost:<port_2>.
    • Example adb pair
    • Run adb devices.
    • Run adb tcpip 5555.
    For Android < 11, you *MUST have PC/Laptop
    • Go to your phone Settings.

    • Find Developer Mode.

    • Enable Developer Mode.

    • Follow me this step.

      Settings 1 Settings 2
      Settings 1 Settings 2
    • If your device doesn't match or is not similar to my phone, then try this solution.

    • Connect your phone to PC/Laptop using a USB cable.

    • On PC/Laptop, open the shell with administrator.

    • Next, you need to install Choco (This is a very easy way to install adb).

    • Run Get-ExecutionPolicy.

    • Run Set-ExecutionPolicy AllSigned.

    • Run Set-ExecutionPolicy Bypass -Scope Process.

    • Run Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')).

    • After installing Choco, run choco install adb.

    • Open the command prompt on PC/Laptop, run adb devices.

    • Then continue run adb tcpip 5555.

    • *And run this (I don't know if it's very important or not, but my Oppo phone needs this to run successfully) to allow termux write secure settings adb shell pm grant com.termux android.permission.WRITE_SECURE_SETTINGS.

    • Now you can unplug the USB cable.

Recheck Step

  • Make sure you have enabled the ADB Server.
  • Open Termux.
  • Run adb kill-server.
  • Then run adb devices.
  • Example adb devices
  • Make sure you only see emulator-5554 in the list.

Important

  • If you turn off or restart your device, you must enable ADB Server AGAIN.
  • If you close termux app and open again, you need to active adb server by run adb devices.

Example Code For Chromium

  • This apk only support arm64, for 32 bit i dont know.

  • Download link (current selenium only support chromium <= 110).

  • Or use this link for fast download.

  • Make sure you CLOSED all chromium instances before run this code (Chrome, Chrome Dev, Chrome Canary, Kiwi Browser, Brave, ....).

    import os
    os.system("adb devices")
    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_experimental_option("androidPackage", "org.chromium.chrome.stable")
    driver = webdriver.Chrome(options=options)
    driver.get("https://www.google.com")
    print("Page title:", driver.title)
    driver.quit()
    

Missing JAVA

  • This means you must install JAVA.
    cd $HOME
    wget https://github.com/lzhiyong/termux-ndk/releases/download/openjdk/openjdk-11.0.12-aarch64.zip
    unzip openjdk-11.0.12-aarch64.zip -d openjdk-11.0.12
    rm -r openjdk-11.0.12-aarch64.zip
    echo "export PATH=$PATH:$HOME/openjdk-11.0.12/bin" >> $HOME/.bashrc
    echo "export JAVA_HOME=$HOME/openjdk-11.0.12" >> $HOME/.bashrc
    

References