51Testing软件测试论坛

标题: 求问怎么解决 Mac+Jenkins 中 selenium 启动 chromedriver 未显示浏览器的问题 [打印本页]

作者: 测试积点老人    时间: 2020-6-15 11:18
标题: 求问怎么解决 Mac+Jenkins 中 selenium 启动 chromedriver 未显示浏览器的问题
本地执行脚本没问题,能正常打开浏览器,并通过点击对应的控件元素,但是集成到Jenkins之后,在Jenkins执行时,因为浏览器未打开显示出来,导致后续的点击元素失败。请问人遇到这样的情况么?是需要在Jenkins中配置什么环境么?脚本如下:
  1. import os
  2. import requests
  3. import json
  4. import time
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. import selenium.webdriver.support.expected_conditions as EC


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


  31. # 获取cookies信息
  32. def get_cookies(driver):
  33.     cookies = driver.get_cookies()
  34.     Cookies = ''
  35.     for lt in cookies:
  36.         Cookies = Cookies + lt['name'] + '=' + lt['value'] + ';'
  37.     print('Cookies:' + str(Cookies))
  38.     driver.quit()
  39.     return Cookies


  40. driver = get_driver("http://www.baidu.com")
  41. cookies = get_cookies(driver)
复制代码
1、本地执行结果
  1. $ python3 test.py
  2. current_dir: /Users/aaa/JenkinProjects
  3. chromedriver_dir: /Users/aaa/JenkinProjects/chromedriver
  4. Click success
  5. 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执行结果
  1. Running as SYSTEM
  2. Building in workspace /Users/Shared/Jenkins/Home/workspace/test
  3. [test] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins5191306205202886733.sh
  4. + /usr/local/Cellar/python/3.7.0/bin/python3 /Users/aaa/JenkinProjects/test.py
  5. current_dir: /Users/aaa/JenkinProjects
  6. chromedriver_dir: /Users/aaa/JenkinProjects/chromedriver
  7. Traceback (most recent call last):
  8.   File "/Users/aaa/JenkinProjects/test.py", line 71, in <module>
  9.     driver = get_driver("http://www.baidu.com")
  10.   File "/Users/aaa/JenkinProjects/test.py", line 55, in get_driver
  11.     driver.find_element_by_id('su').click()
  12.   File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
  13.     self._execute(Command.CLICK_ELEMENT)
  14.   File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
  15.     return self._parent.execute(command, params)
  16.   File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
  17.     self.error_handler.check_response(response)
  18.   File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
  19.     raise exception_class(message, screen, stacktrace)
  20. selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  21.   (Session info: chrome=83.0.4103.97)
  22.   (Driver info: chromedriver=70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778),platform=Mac OS X 10.14.5 x86_64)

  23. Build step 'Execute shell' marked build as failure
  24. Finished: FAILURE
复制代码




作者: 郭小贱    时间: 2020-6-16 09:26
参考这篇帖子看下:https://testerhome.com/topics/24246
作者: bellas    时间: 2020-6-16 09:51
来占沙发
作者: 海海豚    时间: 2020-6-16 09:52
看下driver版本是否对应
作者: qqq911    时间: 2020-6-16 10:49
检查下版本匹配成都
作者: jingzizx    时间: 2020-6-16 12:37
确认不能开启的原因




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2