Chrome

https://linuxize.com/post/how-to-install-google-chrome-web-browser-on-centos-7/

  1. cat > /etc/yum.repos.d/google-chrome.repo <<EOF
  2. [google-chrome]
  3. name=google-chrome
  4. baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
  5. enabled=1
  6. gpgcheck=1
  7. gpgkey=https://dl.google.com/linux/linux_signing_key.pub
  8. EOF
  9. yum install -y google-chrome-stable
  10. google-chrome --version
  11. # Google Chrome 105.0.5195.125

ChromeDriver

https://chromedriver.chromium.org/downloads

  • 下载对应版本的驱动程序

图片.png 图片.png

  1. wget https://chromedriver.storage.googleapis.com/105.0.5195.52/chromedriver_linux64.zip
  2. # 解压至PATH路径下,否则后面使用selenium时需手动指定
  3. unzip chromedriver_linux64.zip
  4. mv chromedriver /usr/local/bin/

Selenium

https://pypi.org/project/selenium/

  1. python3 -m pip install selenium

headless模式

  1. from selenium import webdriver
  2. # service
  3. chromedriver = './chromedriver'
  4. service = webdriver.chrome.service.Service(chromedriver)
  5. # options
  6. options = webdriver.ChromeOptions()
  7. options.add_argument('--headless')
  8. # init browser
  9. browser = webdriver.Chrome(service=service, options=options)
  10. # get url
  11. browser.get('https://www.selenium.dev')
  12. # find element
  13. e = browser.find_element(by='id', value='td-cover-block-0')
  14. print(e.text)
  15. # screenshot
  16. browser.save_screenshot('screenshot.png')
  17. # quit
  18. browser.quit()