A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

火狐默认的版本为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进行操作,就是以本地用户在进行操作


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马