Skip to content

Instantly share code, notes, and snippets.

@xd003
Last active April 21, 2024 13:33
Show Gist options
  • Select an option

  • Save xd003/58cdaadb826149e28bb81fdffc8b9091 to your computer and use it in GitHub Desktop.

Select an option

Save xd003/58cdaadb826149e28bb81fdffc8b9091 to your computer and use it in GitHub Desktop.
Bash Script to setup Python + Selenium + chromedriver on a Headless Chrome Browser
#!/usr/bin/env bash
echo "This script will setup webdriver of your choice for selenium and create a template script with appropriate settings"
sleep 2
echo "This Script only supports Linux 64 bit and arm64/aarch64 kernel architecture currently"
sleep 2
uname="$(uname -m)"
case $uname in
arm64|aarch64|x86_64 )
echo "linux kernel architecture supported , Continuing..";;
* )
echo "Unsupported kernel architecture"
exit;;
esac
# install all necessary packages common to all architectures
sudo apt install unzip python3 python3-distutils python3-pip jq -y && sudo python3 -m pip install -U selenium
while true; do
echo
echo "Enter 1 for Chromedriver + Chromium"
echo "Enter 2 for Geckodriver + Firefox"
read -p "Enter your input : " input </dev/tty
case $input in
1|2 )
break;;
* )
echo "Invalid Input";;
esac
done
#creating variables
loc1="$(which chromedriver)"
loc2="$(which chromium-browser)"
loc3="$(which geckodriver)"
lgecko="$(curl -s 'https://api.github.com/repos/mozilla/geckodriver/releases/latest' |
jq -r '.assets | .[] | .browser_download_url' |
grep linux64)"
shopt -s extglob
case $uname+$input in
@(aarch64|arm64)+@(1) )
sudo apt remove chromium-browser && sudo rm -rf $loc1 && \
sudo add-apt-repository ppa:saiarcot895/chromium-dev && \
sudo apt-get update && \
sudo apt-get install chromium-browser && \
mkdir temp && cd temp && \
wget https://github.com/electron/electron/releases/download/v10.0.0-beta.4/chromedriver-v10.0.0-beta.4-linux-arm64.zip && \
unzip chromedriver* && \
chmod +x chromedriver && \
sudo mv chromedriver /usr/local/bin/ && \
sudo chown root:root /usr/local/bin/chromedriver && \
sudo chmod 0755 /usr/local/bin/chromedriver && \
cd .. && rm -rf temp && \
cat > template.py << endmsg
#!/usr/bin/env python3
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
options.binary_location = '$loc2'
driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')
driver.get("https://www.google.com")
print(driver.title)
print ("Headless Chrome Initialized")
driver.quit()
endmsg
echo "template.py created with appropriate options to run chromium headless with chromedriver" && \
echo "Run python3 template.py to execute the script";;
@(x86_64)+@(1) )
sudo apt install chromium-chromedriver && \
cat > template.py << endmsg
#!/usr/bin/env python3
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', options=options)
driver.get("https://www.google.com")
print(driver.title) # results
print ("Headless Chrome Initialized")
driver.quit()
endmsg
echo "template.py created with appropriate options to run chromium headless with chromedriver" && \
echo "Run python3 template.py to execute the script";;
@(arm64|aarch64)+@(2) )
sudo rm -rf $loc3 && sudo apt install firefox && \
wget http://launchpadlibrarian.net/482741892/firefox-geckodriver_77.0.1+build1-0ubuntu0.20.04.1_arm64.deb && \
sudo dpkg -i firefox-gecko* && rm -rf firefox-gecko* && \
cat > template.py << endmsg
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get('http://google.com')
print(driver.title)
driver.quit()
endmsg
echo "template.py created with appropriate options to run Firefox headless with geckodriver" && \
echo "Run python3 template.py to execute the script";;
@(x86_64)+@(2) )
sudo rm -rf $loc3 && sudo apt install firefox && \
wget $lgecko && \
tar -xvzf geckodriver* && \
chmod +x geckodriver && \
sudo mv geckodriver /usr/local/bin/ && \
sudo chown root:root /usr/local/bin/geckodriver && \
sudo chmod 0755 /usr/local/bin/geckodriver && \
rm -rf geckodriver* && \
cat > template.py << endmsg
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get('http://google.com')
print(driver.title)
driver.quit()
endmsg
echo "template.py created with appropriate options to run Firefox headless with geckodriver" && \
echo "Run python3 template.py to execute the script";;
esac
@cristianvillalobossilvacl
Copy link
Copy Markdown

firefox fix arm64 armbian s905 processor
https://packages.debian.org/sid/arm64/gcc-9-base/download
First, add the link in the page https://packages.debian.org/sid/arm64/gcc-9-base/download to /etc/apt/sources.list Second, run apt --fix-broken install
nano /etc/apt/sources.list
deb [trusted=yes] http://ftp.de.debian.org/debian sid main
apt --fix-broken
apt install gcc-11
chmod 775 selenium-setup.sh
./selenium-setup.sh
option2 for firefox "Enter 2 for Geckodriver + Firefox"

thank you for the script

@cristianvillalobossilvacl
Copy link
Copy Markdown

also make apt update very important

@ymerouani
Copy link
Copy Markdown

Any chance of updating the script, Im trying to run Selenium on an ARM64 VM and it seems to be impossible :(

@yuanjv
Copy link
Copy Markdown

yuanjv commented Sep 6, 2022

Any chance of updating the script, Im trying to run Selenium on an ARM64 VM and it seems to be impossible :(

same. i spend at least 5 hours. because im useing a non raspberrypi arm64 computer, i just cant make Selenium possible.

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