资源可以来这里下载,版本是对应好的,按照博客即可安装运行

1.安装谷歌浏览器
yum localinstall google-chrome-stable.rpmgoogle-chrome-stable.rpm
2.配置webdriver
/usr/bin
3.安装selenium grid
hub_config.jsonnode_config.json
{
  "port": 4455,
  "newSessionWaitTimeout": -1,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {},
  "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "registry": "org.openqa.grid.internal.DefaultGridRegistry",
  "throwOnCapabilityNotPresent": true,
  "cleanUpCycle": 5000,
  "role": "hub",
  "debug": false,
  "browserTimeout": 0,
  "timeout": 1800
}

{
  "capabilities":
  [
    {
      "browserName": "firefox",
      "marionette": true,
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "chrome",
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "internet explorer",
      "platform": "WINDOWS",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "safari",
      "technologyPreview": false,
      "platform": "MAC",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
  "maxSession": 5,
  "port": -1,
  "register": true,
  "registerCycle": 5000,
  "hub": "http://localhost:4455",
  "nodeStatusCheckTimeout": 5000,
  "nodePolling": 5000,
  "role": "node",
  "unregisterIfStillDownAfter": 60000,
  "downPollingLimit": 2,
  "debug": false,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {}
}

进入目录,分别使用命令

java -jar selenium-server-standalone-3.141.59.jar -role hub -hubConfig hub_config.json

java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig node_config.json

即可运行,访问ip:4455即可出现gird控制台

4.Python代码测试

这里需要注意的是添加配置,不然会报错,可能的原因是服务器无界面

from selenium import webdriver



chrome_capabilities = {
    "browserName": "chrome",        # 浏览器名称
    "version": "",                  # 操作系统版本
    "platform": "ANY",              # 平台,这里可以是windows、linux、andriod等等
    "javascriptEnabled": True,      # 是否启用js
}

# 配置谷歌浏览器静默启动【后台启动】
chrome_options = webdriver.ChromeOptions()
# 初始化谷歌浏览器的配置【必须加上】
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')


driver = webdriver.Remote("http://ip:4455/wd/hub",
                          desired_capabilities=chrome_capabilities,
                          options = chrome_options)
driver.get("https://www.baidu.com")
print(driver.title)

# 运行一次结束