arm64 ubuntu使用 Selenium

默认分类 · 2023-09-04

下载相关文件,这里是20.04,在这个面页:https://launchpad.net/ubuntu/+source/chromium-browser/1:85.0.4183.83-0ubuntu0.20.04.3/+build/25538391

# dependencies
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+files/chromium-codecs-ffmpeg_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+files/chromium-codecs-ffmpeg-extra_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb

# chromium-browser
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+files/chromium-browser_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb

# chromium-chromedriver
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+files/chromium-chromedriver_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb

# Install all
apt-get update
apt-get install -y ./chromium-codecs-ffmpeg_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb 
apt-get install -y. /chromium-codecs-ffmpeg-extra_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb 
apt-get install -y ./chromium-browser_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb 
apt-get install -y ./chromium-chromedriver_96.0.4664.110-0ubuntu0.18.04.1_arm64.deb

# Install selenium
pip install -U pip
pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

def main():
    # 指定Chromedriver的路径
    chromedriver_path = '/usr/bin/chromedriver'

    # 创建Chromedriver的服务对象,并指定Chromedriver路径
    chrome_service = webdriver.chrome.service.Service(chromedriver_path)

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')  # 根据需要添加其他选项

    # 创建带有服务对象和选项的WebDriver实例
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

    try:
        driver.get('https://www.google.com/')
        print(driver.title)
        search = driver.find_element(By.NAME, 'q')
        search.send_keys('Python')
        search.send_keys(Keys.RETURN)

        time.sleep(3)
        driver.save_screenshot('search.png')
    finally:
        driver.quit()

if __name__ == '__main__':
    main()
Theme Jasmine by Kent Liao