尝试在詹金斯上运行硒时出现问题

时间:2020-09-03 11:02:34

标签: selenium jenkins selenium-chromedriver

1,在Ubuntu上设置詹金斯 2,在ubuntu上设置硒 3,创建硒测试脚本 代码如下:

from selenium import webdriver
import time

driver=webdriver.Chrome(executable_path='/home/test/chromedriver')
driver.get('https://www.yahoo.com')
time.sleep(2)
screenshot=driver.save_screenshot("yahoo.png")
driver.quit()

4,成功在本地执行脚本 5,尝试使用詹金斯运行脚本, 在詹金斯建立执行壳, cd / home / test python test.py 6,詹金斯打造失败 控制台输出:

Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/test
[test] $ /bin/sh -xe /tmp/jenkins127263638261616263.sh
+ cd /home/test
+ python test.py
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    driver=webdriver.Chrome(executable_path='/home/test/chromedriver')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
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.)

Build step 'Execute shell' marked build as failure
Finished: FAILURE

似乎Webdriver无法启动,不确定如何解决此问题并在jeninks上运行selenium。自动化的新手,请多多指教!

2 个答案:

答案 0 :(得分:0)

我认为问题出在chromedriver路径。此路径是否存在于jenkins节点“ executable_path ='/ home / test / chromedriver'”上?

提供chromediver的正确路径,该路径可在jenkins节点中访问。

答案 1 :(得分:0)

尝试在硒测试脚本中添加以下内容:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument('--headless')