# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"
# apt update
# apt upgrade
# wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# apt-get install -y ./google-chrome-stable_current_amd64.deb
# google-chrome --version
Google Chrome 123.0.6312.86
# pip3 install selenium
# pip3 install webdriver-manager
# vim test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options ()
options.add_argument( ' --headless ' )
options.add_argument( ' --no-sandbox ' )
options.add_argument( ' --disable-dev-shm-usage ' )
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get( " https://python.org " )
print(driver.title)
driver.close ()
# vim test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options ()
# options.add_argument('--headless')
# options.add_argument('--no-sandbox')
options.add_argument( ' --disable-dev-shm-usage ' )
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get( " https://python.org " )
print(driver.title)
driver.close ()
# python3 test.py
Welcome to Python.org
*/1 * * * * export DISPLAY=:0; python ~/selenium_script.py
# find / -name "chromedriver"
/root/.cache/selenium/chromedriver
/root/.cache/selenium/chromedriver/linux64/117.0.5938.92/chromedriver
/root/.wdm/drivers/chromedriver
/root/.wdm/drivers/chromedriver/linux64/117.0.5938.92/chromedriver-linux64/chromedriver
*/05 * * * * export PATH=$PATH:/root/.wdm/drivers/chromedriver/linux64/117.0.5938.92/chromedriver-linux64/chromedriver; root /data/test.py
หรือคุณสามารถสร้างสคริปต์ Bash ที่โหลดสคริปต์ Python และระบุเส้นทาง:
# vim test_loader.sh
#!/bin/bash
export PATH=$PATH:/root/.wdm/drivers/chromedriver/linux64/117.0.5938.92/chromedriver-linux64/chromedriver
...
...
python3 test.py
(1)
from selenium import webdriver
driver = webdriver.Chrome('/home/user/drivers/chromedriver')
(2)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
ตัวจัดการการติดตั้ง:
pip install webdriver-manager
ใช้กับโครม
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
For Mac M1, download mac-arm64.
- For Chrome version: https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.141/mac-arm64/chromedriver-mac-arm64.zip
For General Linux distributions, download linux64.
- For Chrome version: https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.141/linux64/chromedriver-linux64.zip
สำหรับ Mac ตำแหน่งเริ่มต้นสำหรับ Chromedriver คือ /usr/local/bin
% sw_vers
ProductName: macOS
ProductVersion: 13.0
BuildVersion: 22A380
% ls -al /usr/local/bin/chromedriver
-rwxr-xr-x 1 root wheel 17314928 Mar 25 13:23 /usr/local/bin/chromedriver
หากคุณต้องการวางไว้ในไดเร็กทอรีที่กำหนดเอง:
from selenium import webdriver
from selenium . webdriver . chrome . service import Service
def chrome_webdriver ():
chromedriver_path = '/Users/mymac/data/chromedrv/chromedriver'
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/123.0.0.0 Safari/537.36'
options = webdriver . ChromeOptions ()
options . add_argument ( "--start-maximized" )
options . add_argument ( '--headless' )
options . add_argument ( f'user-agent= { user_agent } ' )
service = Service ( executable_path = chromedriver_path )
driver = webdriver . Chrome ( service = service , options = options )
return driver
url = 'http://www.google.com'
driver = chrome_webdriver ()
driver . get ( url )
driver . implicitly_wait ( 10 )
print ( driver . page_source )
ใช้เครื่องหมายฉลาด:
# apt-mark hold google-chrome-stable
google-chrome-stable set on hold.
# apt-mark unhold google-chrome-stable
Canceled hold on google-chrome-stable.