linux 安装浏览器和驱动

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/baidu_39372836/article/details/98619848

linux 安装浏览器和驱动

1、火狐浏览器及驱动-centos6 64

  • 火狐浏览器-在centos6.10自带,而且浏览器版本为52版本

  • 下载对应的火狐驱动,52版本-对应0.17版本-查看地址

  • 添加到环境变量

    # 解压
    tar -zxvf geckodriver-v0.17.0-linux64.tar.gz 
    
    # 新建文件夹
    mkdir /usr/local/seleniumdriver
    
    # 移动到文件夹
    mv geckodriver /usr/local/seleniumdriver
    
    # 编辑/ect/profile文件
    vim /etc/profile
    
    # 在文件最后添加
    export PATH=$PATH:/usr/local/seleniumdriver
    
    # 保存退出运行
    source /etc/profile
    
    # 运行geckodriver -V
    geckodriver 0.17.0
    
    

    注意:运行项目最好放在英文目录下

2、火狐浏览器及驱动-centos7 64

  • 火狐默认的版本为60版本

  • 使用0.19的火狐驱动,解压保存在/usr/local/seleciumdriver 文件夹中

  • 添加到/etc/profile文件中

    # 编辑文件
    vim /etc/profile
    
    # 浏览器驱动
    export PATH=$PATH:/usr/local/seleniumdriver
    
    # 保存退出运行
    source /etc/profile
    
    # 运行geckodriver -V
    geckodriver 0.19.0
    
  • 运行时,可以通过当前目录生成geckodriver.log 文件查看error

    • 以root用户运行直接运行:python 123.py

      报错:

      selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1
      

      坑点:这通过查看geckodriver.log可以发现:

      1565086641745	geckodriver	INFO	geckodriver 0.19.0
      1565086641748	geckodriver	INFO	Listening on 127.0.0.1:56020
      1565086641834	mozrunner::runner	INFO	Running command: "/usr/lib64/firefox/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.3LBcykrEU73V"
      Running Firefox as root in a regular user's session is not supported.  ($XDG_RUNTIME_DIR is /run/user/1000 which is owned by admin.)
      
      

      上面错误,不支持在普通用户的会话中以root身份运行Firefox。解决方法:切换到其他用户执行即可。

    • 测试代码

      import time
      from selenium import webdriver
      driver = webdriver.Firefox()
      driver.get("http://www.baidu.com")
      time.sleep(3)
      driver.quit()
      
      

      在远程服务器直接运行没问题,但是在win10 pycharm进行远程运行时报错:

      selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
      

      两个问题:1、无法发现火狐驱动 2、使用远程连接时(使用pycharm或者其他连接工具xshell均是如此),是默认连接无界面的linux,导致无法启动,可以修改代码为:

      import time
      from selenium import webdriver
      # 设置为无头模式
      profile = webdriver.FirefoxOptions()
      profile.add_argument("-headless")
      # 指定火狐的目录
      driver =webdriver.Firefox(executable_path="/usr/local/seleniumdriver/geckodriver",options=profile)
      driver.get("http://www.baidu.com")
      print(driver.page_source)
      time.sleep(3)
      driver.quit()
      
      

      重点1:目前发现部署测试项目到linux有界面的服务器上,需要添加无头模式以及指定驱动位置

      重点2:在VM中的有界面linux,登录上去之后,直接运行则无需(添加无头模式以及指定驱动位置两个操作),所以也可以用win连接linux桌面,再进行执行代码即可

      重点3:本来部署有界面的服务器,就是为了能在服务器跑,同时有界面的运行更加符合预期,所以尝试结合jenkins进行操作,就是以本地用户在进行操作

3、谷歌浏览器及驱动的安装-centos7 64

  • 下载 谷歌浏览器下载
  • 下载文件名为rpm结尾安装包-google-chrome-stable_current_x86_64.rpm
  • 输入命令进行安装yum localinstall google-chrome-stable_current_x86_64.rpm
    • 安装的版本为:Google Chrome76.0.3809.87 (正式版本) (64 位)
  • 安装驱动,同上步骤,寻找对应的版本进行解压,放置在/usr/local/seleciumdriver 文件夹中
  • 刷新权限source /etc/profile
  • 验证chromedriver --version
  • 运行代码即可。

猜你喜欢

转载自blog.csdn.net/baidu_39372836/article/details/98619848