Selenium 无法在 Ubuntu Jenkins 上使用预设配置文件运行 Chrome。

huangapple go评论100阅读模式
英文:

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 &quot;/var/lib/jenkins/workspace/Wip_PY/pytest/tests/wip/test_wip.py&quot;, line 28, in &lt;module&gt;
    driver = webdriver.Chrome(options=chrome_options)
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chrome/webdriver.py&quot;, line 49, in __init__
    super().__init__(
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/chromium/webdriver.py&quot;, line 54, in __init__
    super().__init__(
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py&quot;, line 206, in __init__
    self.start_session(capabilities)
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py&quot;, line 291, in start_session
    response = self.execute(Command.NEW_SESSION, caps)[&quot;value&quot;]
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/webdriver.py&quot;, line 346, in execute
    self.error_handler.check_response(response)
  File &quot;/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py&quot;, 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 &lt;unknown&gt;
#1 0x55dc0d35dc76 &lt;unknown&gt;
#2 0x55dc0d38a415 &lt;unknown&gt;
#3 0x55dc0d384419 &lt;unknown&gt;
#4 0x55dc0d383029 &lt;unknown&gt;
#5 0x55dc0d3c1ccc &lt;unknown&gt;
#6 0x55dc0d3c147f &lt;unknown&gt;
#7 0x55dc0d3b8de3 &lt;unknown&gt;
#8 0x55dc0d38e2dd &lt;unknown&gt;
#9 0x55dc0d38f34e &lt;unknown&gt;
#10 0x55dc0d5ee3e4 &lt;unknown&gt;
#11 0x55dc0d5f23d7 &lt;unknown&gt;
#12 0x55dc0d5fcb20 &lt;unknown&gt;
#13 0x55dc0d5f3023 &lt;unknown&gt;
#14 0x55dc0d5c11aa &lt;unknown&gt;
#15 0x55dc0d6176b8 &lt;unknown&gt;
#16 0x55dc0d617847 &lt;unknown&gt;
#17 0x55dc0d627243 &lt;unknown&gt;
#18 0x7f9de9c94b43 &lt;unknown&gt;

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(&#39;--window-size=1920,1080&#39;)
chrome_options.add_argument(&#39;--no-sandbox&#39;)
chrome_options.add_argument(&#39;--disable-gpu&#39;)
chrome_options.add_argument(&#39;--disable-blink-features=AutomationControlled&#39;)
chrome_options.add_argument(&#39;--disable-automation&#39;)
chrome_options.add_argument(&#39;--disable-infobars&#39;)
chrome_options.add_argument(&#39;--ignore-ssl-errors=yes&#39;)
chrome_options.add_argument(&#39;--ignore-certificate-errors&#39;)
chrome_options.add_argument(&#39;--user-data-dir=/home/tester/selenium/google-chrome&#39;)
chrome_options.add_argument(&#39;--profile-directory=Norton&#39;)
chrome_options.add_argument(&#39;--incognito&#39;)
chrome_options.add_experimental_option(&#39;excludeSwitches&#39;, [&#39;enable-automation&#39;])
chrome_options.add_experimental_option(&#39;excludeSwitches&#39;, [&#39;enable-logging&#39;])
chrome_options.set_capability(&#39;pageLoadStrategy&#39;, &#39;eager&#39;)


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(&quot;--headless&quot;)
chrome_options.add_argument(&quot;--disable-dev-shm-usage&quot;)

huangapple
  • 本文由 发表于 2023年7月13日 23:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定