一、安装selenium

sudo pip3 install selenium

二、安装Chrome浏览器

  • 安装依赖

    sudo apt-get install libxss1 libappindicator1 libindicator7
  • 下载安装包

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb #执行命令,下载稳定版Chrome浏览器
  • 安装

    sudo dpkg -i google-chrome*.deb
    sudo apt-get install -f
  • 三、安装chromedriver

  • 查看Chrome浏览器版本

    google-chrome --version   #执行该命令获取当前Chrome浏览器版本号
  • 下载对应版本chromedriver

    wget -N http://chromedriver.storage.googleapis.com/浏览器版本号(比如88.0.4324.96)/chromedriver_linux64.zip
  • 安装unzip,用于解压缩

    sudo apt-get install unzip
  • 解压缩

    unzip chromedriver_linux64.zip
  • 移动chromedriver位置

    sudo mv chromedriver /usr/local/share/chromedriver
  • 建立软链接-----后续创建driver时就不需要再指定executable_path这个参数

    sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
  • Caused by: org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist
  • 如果报上述错误,chromedriver的配置要改成如下
  • this.driverUrl = driverUrl;
    //1.创建chrome的配置信息
    System.setProperty("webdriver.chrome.driver",driverUrl);
    ChromeOptions chromeOptions =new ChromeOptions();
    //2.设置为headless模式(必须)  如果不写代表不打开浏览器,反之
    chromeOptions.addArguments("--headless");
    // 禁用沙箱   linux环境
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--disable-dev-shm-usage");
    //3.设置浏览器窗口打开大小 (非必须)
    chromeOptions.addArguments("--window-size=1920,1080");
    driver = new ChromeDriver(chromeOptions);
Logo

更多推荐