在linux环境下怎样使用基于python的selenium进行爬虫自动化测试

本文介绍了如何在Linux服务器上安装和配置Selenium爬虫,包括安装谷歌浏览器、匹配并安装对应的Chrome驱动,以及处理运行时的权限和异常问题。特别地,提到了使用无头模式来运行爬虫,并展示了设置无头模式的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

    本笔记适合有一定python基础的编程人员查看。

    并且在笔记末尾记录了遇到的各种问题以及解决办法

问题:

    在本地电脑上写好selenium的爬虫自动化脚本之后,需要放到linux中运行。那在linux_server中应该安装哪些依赖呢?

解决记录:

    1、安装谷歌:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

    2、检查谷歌浏览器的版本:google-chrome --version

    3、根据2中的版本,在以下地址中找到最接近我们安装版本的驱动。

        安装谷歌浏览器驱动:https://npm.taobao.org/mirrors/chromedriverCNPM Binaries Mirrorhttps://npm.taobao.org/mirrors/chromedriver

        最接近的版本需要同时满足以下条件:

        从前往后的版本号,能一摸一样的就选一摸一样的.

        最后一个版本号,需要大于或者等于驱动的版本号

        例如我根据2中查到的结果是:        

[root@localhost tmp_delete]# google-chrome --version
Google Chrome 114.0.5735.198 
[root@localhost tmp_delete]# 

        其中114.0.5735.198就是我的环境上的版本号

        到驱动下载中查看结果如下

         需要确保114.0.5735部分一样,最后的部分找跟198最接近的数字,就找到如下这个版本

    点进去之后下载

 下载之后的驱动,可以自已存放到任意位置,在代码中指定该驱动的位置即可

额外说明:

  需要将selenium设置为无头模式

设置方法为:

  

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


class Crawl:
    def __init__(self):
        options = Options()
        options.add_argument('--headless')
        self.wb = webdriver.Chrome(executable_path='./chromedriver',options=options)



    def login(self,username,passwd):
        self.wb.find_element(by=By.XPATH,value='//*[@id="userName"]').clear()
        # 输入账号
        self.wb.find_element(by=By.XPATH,value='//*[@id="userName"]').send_keys(username)

遇到的问题   

问题1

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

解决:

给驱动增加可执行权限

chmod +x chromedriver 

[root@localhost 114.0.5735.90]# ls -l 
总用量 27488
-rw-------. 1 root root 15039112 5月  27 08:10 chromedriver
-rw-------. 1 root root     4013 7月  13 14:55 crawlFromWeb.py
[root@localhost 114.0.5735.90]# 
[root@localhost 114.0.5735.90]# 
[root@localhost 114.0.5735.90]# chmod +x chromedriver 
[root@localhost 114.0.5735.90]# 
[root@localhost 114.0.5735.90]# 
[root@localhost 114.0.5735.90]# 
问题2:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

解决:

options添加参数options.add_argument('--no-sandbox')

options.add_argument('--no-sandbox')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JamesDanni

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值