Issue
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
Solution
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")
Answered By - Rafael C. Answer Checked By - Terry (WPSolving Volunteer)