Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HunterXProgrammer/beb06a5387bee78ee321fbe3c724d95e to your computer and use it in GitHub Desktop.
Save HunterXProgrammer/beb06a5387bee78ee321fbe3c724d95e to your computer and use it in GitHub Desktop.

https://www.reddit.com/r/tasker/comments/14e445n/comment/jozkd3t/

yes | pkg upgrade -y

pkg install -y tur-repo x11-repo python-pip

pkg install -y chromium

pip install selenium

If you know how i would love to get some info about Selenium and Android.

From what i get you can't just use Selenium with just Termux and Chromium on Android. If you have some knowledge how to do that i would love to hear!

No worries, it's all about trying a little at first and slowly becoming used to it!

I'll give here a python script showing how to -

1) Use selenium in Chromium or Firefox

2) Open Reddit login page

3) Make the layout like as if viewing in mobile

4) Enter Username and Password and click Login button

5) Dismiss the Reddit prompt that tells use to use their app

6) Load the "Comments you have received" setion, which is only visible after a successful login.

7) And take a screenshot and save it to Download/screenshot.png

 

For Chromium

Setup once:-

termux-setup-storage

yes | pkg upgrade -y

pkg install -y tur-repo x11-repo python-pip

pkg install -y chromium

pip install selenium

Now to write and save the python script, do -

nano reddit_screenshot_chromium.py

Press enter, and then copy paste this -

#!/data/data/com.termux/files/usr/bin/python

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()

options.add_argument("--headless=new")
options.add_argument("--incognito")
options.add_argument("--hide-scrollbars")

# Setting User-Agent to mobile to get pages as if viewed in mobile
options.add_argument('--user-agent="Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"')

try:
    # Start Headless Chromium
    print("\n  Launching Chromium (Headless mode)\n   (No GUI = no resources wasted)\n")
    driver = webdriver.Chrome(options=options)
    
    # Setting browser size to more mobile-like dimensions
    print("  Setting browser size to more mobile-like dimensions\n")
    driver.set_window_size(driver.get_window_size()['width']/2, driver.get_window_size()['height'])
    
    # Loading url https://www.reddit.com/login
    print("  Loading url https://www.reddit.com/login\n")
    driver.get("https://www.reddit.com/login")
    
    # Reddit uses iframe for login box, so switching to it. No needed for most other sites, this is for demonstration
    driver.switch_to.frame(WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='container']/div/div[1]/div[1]/iframe"))))
    
    # Selecting input field, and entering first argument as Username
    print("  Selecting Username field and entering Username\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#loginUsername"))).send_keys(sys.argv[1])
    
    # Selecting input field, and entering second argument as Password
    print("  Selecting Password field and entering Password\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#loginPassword"))).send_keys(sys.argv[2])
    
    # Clicking the Login button
    print("  Clicking the Login button\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.AnimatedForm__submitButton.m-full-width.m-modalUpdate"))).click()
    
    # Switching back from previous iframe to current window
    driver.switch_to.window(driver.window_handles[0])
    
    # Reddit always gives annoying prompt telling us to try their app. This will click continue in browser option
    print("  Dismissing the Reddit prompt where they tell us to\n   use their app\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.XPromoPopupRpl__actionButton"))).click()
    
    # Loading the "Comments you have received" section, which is visible only after successful login
    print('  Loading the "Comments you have received" section,\n   which is visible only after successful login\n')
    driver.get("https://www.reddit.com/notification/comments")
    
    # Taking screenshot and saving it to /sdcard/Download/screenshot.png
    # Don't forget to give Termux storage access or else it won't be able to save
    print("  Taking screenshot and saving it to\n   /sdcard/Download/screenshot.png\n")
    driver.save_screenshot("/sdcard/Download/screenshot.png")
    print("  Screenshot saved to Download/screenshot.jpg\n")
    
finally:
    # Exit Headless Chromium
    driver.close()

After that, press CRTL + X + Y + ENTER to save and exit.

Now just append the Reddit username and password when you run the python script, like this -

python reddit_screenshot_chromium.py "some_username" "the_password"

I've made it so that it prints each step so that you'll know the progress of the script.

I've also added a lot of comments for you to understand what each line does.

You can also use Firefox, but I recommend using Chromium since I found it faster (by around 7s) for the same script, as well as having more know-hows.

 

For Firefox

Setup once:-

termux-setup-storage

yes | pkg upgrade -y

pkg install -y tur-repo x11-repo python-pip

pkg install -y firefox geckodriver

pip install selenium

Now to write and save the python script, do -

nano reddit_screenshot_firefox.py

Press enter, and then copy paste this -

#!/data/data/com.termux/files/usr/bin/python

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()

options.add_argument("--headless")
options.add_argument("--private-window")

# Setting User-Agent to mobile to get pages as if viewed in mobile
options.set_preference("general.useragent.override", "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36")

try:
    # Start Headless Firefox
    print("\n  Launching Firefox (Headless mode)\n   (No GUI = no resources wasted)\n")
    driver = webdriver.Firefox(options=options)

    # Setting browser size to more mobile-like dimensions
    print("  Setting browser size to more mobile-like dimensions\n")
    driver.set_window_size(driver.get_window_size()['width']/2, driver.get_window_size()['height'])
    
    # Loading url https://www.reddit.com/login
    print("  Loading url https://www.reddit.com/login\n")
    driver.get("https://www.reddit.com/login")

    # Reddit uses iframe for login box, so switching to it. No needed for most other sites, this is for demonstration
    driver.switch_to.frame(WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='container']/div/div[1]/div[1]/iframe"))))
    
    # Selecting input field, and entering first argument as Username
    print("  Selecting Username field and entering Username\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#loginUsername"))).send_keys(sys.argv[1])
    
    # Selecting input field, and entering second argument as Password
    print("  Selecting Password field and entering Password\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#loginPassword"))).send_keys(sys.argv[2])
    
    # Clicking the Login button
    print("  Clicking the Login button\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.AnimatedForm__submitButton.m-full-width.m-modalUpdate"))).click()
    
    # Switching back from previous iframe to current window
    driver.switch_to.window(driver.window_handles[0])
    
    # Reddit always gives annoying prompt telling us to try their app. This will click continue in browser option
    print("  Dismissing the Reddit prompt where they tell us to\n   use their app\n")
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.XPromoPopupRpl__actionButton"))).click()
    
    # Loading the "Comments you have received" section, which is visible only after successful login
    print('  Loading the "Comments you have received" section,\n   which is visible only after successful login\n')
    driver.get("https://www.reddit.com/notification/comments")
    
    # Taking screenshot and saving it to /sdcard/Download/screenshot.png
    # Don't forget to give Termux storage access or else it won't be able to save
    print("  Taking screenshot and saving it to\n   /sdcard/Download/screenshot.png\n")
    driver.save_screenshot("/sdcard/Download/screenshot.png")
    print("  Screenshot saved to Download/screenshot.jpg\n")
        
finally:
    # Exit Headless Firefox
    driver.close()

After that, press CRTL + X + Y + ENTER to save and exit.

Now just append the Reddit username and password when you run the python script, like this -

python reddit_screenshot_firefox.py "some_username" "the_password"

All done.

 

Selenium is blazing fast when it comes to huge amounts of navigating, element searching, clicking, and so on.

You'll certainly like it even more if you're a power user :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment