查看ubuntu版本

1
2
>> cat /etc/issue
Ubuntu 20.04.6 LTS
  1. 编写测试程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import time


    def test_google_search():
    # 指定 ChromeDriver 的路径
    service = Service("/home/weylan/opt/chromedriver-linux64/chromedriver") # 修改为你的 chromedriver 路径

    # 创建 WebDriver 实例
    # 配置 Chrome 可执行文件路径
    chrome_options = Options()
    chrome_options.binary_location = "/home/weylan/opt/chrome-linux64/chrome" # 修改为 Chrome 的路径
    chrome_options.add_argument("--headless") # 无头模式
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-software-rasterizer")
    chrome_options.add_argument("--disable-gpu") # 禁用 GPU 加速(推荐用于 Linux)
    chrome_options.add_argument("--no-sandbox") # 跳过沙盒模式(避免权限问题)
    chrome_options.binary_location = "/home/weylan/opt/chrome-linux64/chrome" # 修改为你的 Chrome 路径



    # 创建 WebDriver 实例
    driver = webdriver.Chrome(service=service, options=chrome_options)

    try:
    # 打开 baidu 网站
    driver.get("https://www.baidu.com")
    print("访问 baidu 网站成功")

    # # 查找搜索框并输入搜索内容
    # search_box = driver.find_element(By.NAME, "q")
    # search_box.send_keys("Selenium Python")
    # print("输入搜索关键词成功")
    #
    # # 模拟回车键以触发搜索
    # search_box.send_keys(Keys.RETURN)
    # print("触发搜索成功")
    #
    # # 等待页面加载(可以使用显式等待代替)
    # time.sleep(3)
    #
    # # 验证搜索结果是否加载
    # results = driver.find_elements(By.CSS_SELECTOR, "div.g")
    # assert len(results) > 0, "未找到搜索结果"
    # print(f"找到 {len(results)} 个搜索结果")

    finally:
    # 关闭浏览器
    driver.quit()
    print("测试完成,关闭浏览器")


    if __name__ == "__main__":
    test_google_search()

  2. 下载chrome 和driver
    chrome 和driver 的版本要一致,下载后解压到固定目录
    https://googlechromelabs.github.io/chrome-for-testing/

  3. 确认版本兼容性
    确保 selenium 的版本与 chromedriver 和浏览器的版本兼容。更新 Selenium 到最新版本:

    1
    pip install --upgrade selenium
  4. 尝试运行
    这里使用的无ui系统,所以需要尝试启用无头模式,避免GUI问题

    1
    ./chrome --disable-dev-shm-usage --disable-software-rasterizer --disable-gpu --no-sandbox
  5. 赋予执行权限

报错:

1
PermissionError: [Errno 13] Permission denied: '/home/weylan/opt/chromedriver-linux64/chromedriver'

解决:

1
2
3
chmod +x /home/weylan/opt/chromedriver-linux64/chromedriver
chmod +x /home/weylan/opt/chrome-linux64/chrome
chmod +x /home/weylan/opt/chrome-linux64/chrome_crashpad_handler
  1. 安装chrome的依赖库

报错:

1
./chrome: error while loading shared libraries: libgbm.so.1: cannot open shared object file: No such file or directory

解决:

1
2
3
4
5
6
sudo apt update
sudo apt install -y libnss3 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxrandr2 libxss1 libasound2 libatk1.0-0 libpangocairo-1.0-0 fonts-liberation

sudo apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxrandr2 libxdamage1 libxshmfence1 libglu1-mesa libpangocairo-1.0-0 libpango-1.0-0


  1. 处理 D-Bus 和 UPower 错误
    报错:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    [24615:24615:0115/000733.084680:FATAL:spawn_subprocess.cc(237)] posix_spawn /home/weylan/opt/chrome-linux64/chrome_crashpad_handler: Permission denied (13)
    [24614:24626:0115/000733.141974:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24629:0115/000733.142838:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24629:0115/000733.142868:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24626:0115/000733.144556:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.144670:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.144775:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.145086:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.145103:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.147627:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.154498:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
    [24614:24626:0115/000733.154527:ERROR:bus.cc(407)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")

    DevTools listening on ws://127.0.0.1:9222/devtools/browser/e67482e5-95e3-42f4-a6da-5f76012a489a
    [24614:24614:0115/000733.209673:ERROR:object_proxy.cc(576)] Failed to call method: org.freedesktop.DBus.NameHasOwner: object_path= /org/freedesktop/DBus: unknown error type:
    [24614:24714:0115/000733.217328:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24714:0115/000733.217367:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24714:0115/000733.217399:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24714:0115/000733.217419:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24614:24714:0115/000733.217436:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [24639:24639:0115/000733.569645:ERROR:gles2_cmd_decoder_passthrough.cc(1053)] [GroupMarkerNotSet(crbug.com/242999)!:A03028009C280000]Automatic fallback to software WebGL has been deprecated. Please use the --enable-unsafe-swiftshader flag to opt in to lower security guarantees for trusted content.

这些错误主要是由于系统中未运行或未安装 D-Bus 服务和 UPower 服务引起的。

安装和启动 D-Bus 服务:

1
2
3
sudo apt-get install -y dbus
sudo service dbus start

安装 UPower 服务:

1
2
sudo apt-get install -y upower

检查服务是否运行:

1
2
systemctl status dbus
systemctl status upower

如果服务未运行,可以尝试重新启动:

1
sudo systemctl restart dbus

如果在无头服务器上运行,使用以下参数跳过 D-Bus:

1
2
3
4
5
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument("--disable-software-rasterizer")

报错:

WebGL Automatic Fallback

1
2
Automatic fallback to software WebGL has been deprecated.

原因:Chrome 尝试使用软件 WebGL 渲染,但被标记为已弃用。

解决方法:

添加以下启动参数,明确允许使用软件 WebGL:

1
2
3
chrome_options.add_argument("--enable-unsafe-webgl")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-software-rasterizer")

如果不需要 WebGL,可以直接禁用:

1
chrome_options.add_argument("--disable-webgl")