headerless 模式的镜像制作及测试
1 , 运行 centos 镜像
docker run -it –name chrometest docker.io/centos:7 /bin/bash
2, 安装 chrome 最新版本
# wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
# yum install ./google-chrome-stable_current_*.rpm
3, 安装 python3
yum install -y python3
4, 查看版本 google-chrome-stable –version
下载 https://chromedriver.chromium.org/ 根据版本需要
wet https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_linux64.zip
5, wget下载 的时候会 看到 Unable to establish SSL connection
yum install –y openssl
下载就好了,
6, yum install -y unzip
upzip chromedriver_linux64.zip
7. chmod 777 chromedriver
8 . pip3 install selenium
最后,执行测试方法。
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.wait import WebDriverWait # 用于实例化一个Driver的显式等待 from selenium.webdriver.common.by import By # 内置定位器策略集 from selenium.webdriver.support import expected_conditions as EC # 内置预期条件函数,具体API请参考此小节后API链接 chrome_options = Options() chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--no-sandbox") # linux only chrome_options.add_argument("--headless") chrome_options.headless = True # also works driver = webdriver.Chrome(options=chrome_options) driver.get( 'https://www.autoscout24.de/angebote/opel-corsa-c-edition-klima-8xbereift-schiebedach-benzin-silber-018d31ca-5af8-4cd5-8b47-9ee198bca593?cldtidx=20&cldtsrc=listPage') try: WebDriverWait(driver, 200, 0.5).until( EC.presence_of_all_elements_located((By.CLASS_NAME, 'cldt-contact-form-container'))) print(driver.find_element_by_css_selector('.cldt-item .sc-grid-row ').text) print(driver.find_element_by_xpath('/html/body/div[1]/main/div[2]/div[3]/div[2]/div[1]/div[3]/span[3]').text) print(driver.find_element_by_xpath('/html/body/div[1]/main/div[2]/div[3]/div[2]/div[1]/div[3]/span[1]').text) finally: driver.close() # close the driver driver.quit()