英文:
Selenium can't run Chrome with pre set profile on Ubuntu Jenkins
问题
尝试通过Selenium/Python打开指定的Chrome配置文件,但持续收到错误消息。
+ python3 test_wip.py
Traceback (most recent call last):
File "/var/lib/jenkins/workspace/Wip_PY/pytest/tests/wip/test_wip.py", line 28, in <module>
driver = webdriver.Chrome(options=chrome_options)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chrome/webdriver.py", line 49, in __init__
super().__init__(
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chromium/webdriver.py", line 54, in __init__
super().__init__(
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 291, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at /home/tester/selenium/google-chrome is still attached to a running Chrome or Chromium process
Stacktrace:
#0 0x55dc0d62e4e3 <unknown>
#1 0x55dc0d35dc76 <unknown>
#2 0x55dc0d38a415 <unknown>
#3 0x55dc0d384419 <unknown>
#4 0x55dc0d383029 <unknown>
#5 0x55dc0d3c1ccc <unknown>
#6 0x55dc0d3c147f <unknown>
#7 0x55dc0d3b8de3 <unknown>
#8 0x55dc0d38e2dd <unknown>
#9 0x55dc0d38f34e <unknown>
#10 0x55dc0d5ee3e4 <unknown>
#11 0x55dc0d5f23d7 <unknown>
#12 0x55dc0d5fcb20 <unknown>
#13 0x55dc0d5f3023 <unknown>
#14 0x55dc0d5c11aa <unknown>
#15 0x55dc0d6176b8 <unknown>
#16 0x55dc0d617847 <unknown>
#17 0x55dc0d627243 <unknown>
#18 0x7f9de9c94b43 <unknown>
我觉得问题与Jenkins用户有关,因为我可以使用"tester"用户运行以下命令并进入该配置文件:
google-chrome --profile-directory=Norton --user-data-dir=/home/tester/selenium/google-chrome
在没有"--user-data-dir"选项的情况下,其他测试正常运行。以下是代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_argument('--disable-automation')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--ignore-ssl-errors=yes')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--user-data-dir=/home/tester/selenium/google-chrome')
chrome_options.add_argument('--profile-directory=Norton')
chrome_options.add_argument('--incognito')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.set_capability('pageLoadStrategy', 'eager')
driver = webdriver.Chrome(options=chrome_options)
我已尝试更换引号,为测试创建新的Chrome目录,尝试无界面模式。我已检查测试开始时没有Chrome实例。期望Selenium能够在隐身模式下打开所需的配置文件。
英文:
Trying to open specified chrome profile via selenium\python, but keep getting error
+ python3 test_wip.py
Traceback (most recent call last):
File "/var/lib/jenkins/workspace/Wip_PY/pytest/tests/wip/test_wip.py", line 28, in <module>
driver = webdriver.Chrome(options=chrome_options)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chrome/webdriver.py", line 49, in __init__
super().__init__(
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chromium/webdriver.py", line 54, in __init__
super().__init__(
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 291, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at /home/tester/selenium/google-chrome is still attached to a running Chrome or Chromium process
Stacktrace:
#0 0x55dc0d62e4e3 <unknown>
#1 0x55dc0d35dc76 <unknown>
#2 0x55dc0d38a415 <unknown>
#3 0x55dc0d384419 <unknown>
#4 0x55dc0d383029 <unknown>
#5 0x55dc0d3c1ccc <unknown>
#6 0x55dc0d3c147f <unknown>
#7 0x55dc0d3b8de3 <unknown>
#8 0x55dc0d38e2dd <unknown>
#9 0x55dc0d38f34e <unknown>
#10 0x55dc0d5ee3e4 <unknown>
#11 0x55dc0d5f23d7 <unknown>
#12 0x55dc0d5fcb20 <unknown>
#13 0x55dc0d5f3023 <unknown>
#14 0x55dc0d5c11aa <unknown>
#15 0x55dc0d6176b8 <unknown>
#16 0x55dc0d617847 <unknown>
#17 0x55dc0d627243 <unknown>
#18 0x7f9de9c94b43 <unknown>
I feel like problem related to jenkins user, because i can run command as "tester" user "google-chrome --profile-directory=Norton --user-data-dir=/home/tester/selenium/google-chrome" and get into that profile.
Without '--user-data-dir' option other tests runs well. Here is the code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_argument('--disable-automation')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--ignore-ssl-errors=yes')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--user-data-dir=/home/tester/selenium/google-chrome')
chrome_options.add_argument('--profile-directory=Norton')
chrome_options.add_argument('--incognito')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.set_capability('pageLoadStrategy', 'eager')
driver = webdriver.Chrome(options=chrome_options)
I've tried replacing quotes, making new chrome dir for tests, headless mode. Checked that there is no chrome instances when test starts.
Expecting that selenium will open on desired profile in incognito mode
答案1
得分: 0
通常,当在任何CI工具下运行Selenium测试而未设置无头浏览器模式时,会发生此错误。
在这种情况下,您需要添加以下Chrome参数:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
英文:
Usually this error happens when running Selenium tests under any CI tool without setting the headless browser mode.
In this case, you need to add the following Chrome arguments:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论