本地执行脚本没问题,能正常打开浏览器,并通过点击对应的控件元素,但是集成到 Jenkins 之后,在 Jenkins 执行时,因为浏览器未打开显示出来,导致后续的点击元素失败。请问人遇到这样的情况么?是需要在 Jenkins 中配置什么环境么?

脚本如下

import os
import requests
import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC


# 打开网络,登录,获取driver
def get_driver(url):
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    options.add_argument('--hide-scrollbars')
    # options.add_argument('--headless')
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
     # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
    # 设置权限
    prefs = {"profile.managed_default_content_settings.images": 1}
    #options.add_experimental_option("prefs", prefs)
    current_dir = os.path.dirname(os.path.abspath(__file__))
    print("current_dir: " + current_dir)
    chromedriver_dir = current_dir+'/chromedriver'
    print("chromedriver_dir: " + chromedriver_dir)
    driver = webdriver.Chrome(options=options, executable_path=chromedriver_dir)
    driver.get(url)
    # print(driver.page_source)
    driver.find_element_by_id('kw').send_keys('testerhome')
    driver.find_element_by_id('su').click()
    print("Click success")
    return driver


# 获取cookies信息
def get_cookies(driver):
    cookies = driver.get_cookies()
    Cookies = ''
    for lt in cookies:
        Cookies = Cookies + lt['name'] + '=' + lt['value'] + ';'
    print('Cookies:' + str(Cookies))
    driver.quit()
    return Cookies


driver = get_driver("http://www.baidu.com")
cookies = get_cookies(driver)

1、本地执行结果

$ python3 test.py
current_dir: /Users/aaa/JenkinProjects
chromedriver_dir: /Users/aaa/JenkinProjects/chromedriver
Click success
Cookies:WWW_ST=1592104224283;BDSVRTM=15;PSINO=6;BD_CK_SAM=1;H_PS_PSSID=31909_14350_3136_2045_31714_30823_31846;BAIDUID=5945D257E9532E230820BDC4626B9B:FG=1;delPer=0;BIDUPSID=5945D257E5952ECC8F24B6FC2FE77EC;PSTM=159210441;BD_UPN=123253;BD_HOME=1;

2、Jenkins 执行结果

Running as SYSTEM
Building in workspace /Users/Shared/Jenkins/Home/workspace/test
[test] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins5191306205202886733.sh
+ /usr/local/Cellar/python/3.7.0/bin/python3 /Users/aaa/JenkinProjects/test.py
current_dir: /Users/aaa/JenkinProjects
chromedriver_dir: /Users/aaa/JenkinProjects/chromedriver
Traceback (most recent call last):
  File "/Users/aaa/JenkinProjects/test.py", line 71, in <module>
    driver = get_driver("http://www.baidu.com")
  File "/Users/aaa/JenkinProjects/test.py", line 55, in get_driver
    driver.find_element_by_id('su').click()
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=83.0.4103.97)
  (Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Mac OS X 10.14.5 x86_64)

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


↙↙↙阅读原文可查看相关链接,并与作者交流