How to use chromium-browser and chromedriver in VS Code Server in HA

Hi, everyone

I’m a newbie in using home assistant. Please bear with me.

If you have any ideal, please let me know. I will try it.

I try to run python code in VS code server in home assistant. But it is not work. I’ve searched a lot but couldn’t find a solution. I have spent a lot of time on this topic. So I decided to ask here. Maybe someone can give me direction. Or if I still lost somethings to check, please kindly let me know. Thanks.

First, I provde my environment information.

Raspberry pi3
Home Assistant 2023.2.5
Supervisor 2023.01.1Operating System 9.5Frontend 20230202.0 - latest

Python code as below:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)

driver.get('https://www.google.com/')

inputElement = browser.find_element_by_name("q")
inputElement.send_keys("Selenium")
inputElement.submit()
resultLocator = "#rso > div:nth-child(1) > div > div > div.Z26q7c.UK95Uc.jGGQ5e > div > a > h3"
browser.implicity(10)
results = browser.find_elements_by_selector(resultLocator)
print(results.text)
driver.close()

It will use selenium and chromedriver to control web browser to got information I need.
But It didn’t work when I ran it. The error message as below:

Traceback (most recent call last):
  File "/config/python_demo/t1.py", line 10, in <module>
    driver = webdriver.Chrome('chromedriver', options=chrome_options)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/chrome/webdriver.py", line 95, in __init__
    RemoteWebDriver.__init__(
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 152, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 249, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable

I give the path of the chromedriver directly.
driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)

It’s still not work. The error message is the same.

I try to give the path of the chromium-browser

chrome_options.binary_location = '/usr/bin'
driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)

error messge as below:

/config/python_demo/t1.py:11: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)
Traceback (most recent call last):
  File "/config/python_demo/t1.py", line 11, in <module>
    driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/chrome/webdriver.py", line 95, in __init__
    RemoteWebDriver.__init__(
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 152, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 249, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/ is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Base on error message, I confrimed few things by myself

  1. I checked the chromedriver version and chromium browser version. The result that is the same.

  2. I make sure the system environment include /usr/bin. It’s ok.

  3. I confirmed selenium has been installed in the VS Code Server. It’s OK.

Everything seems to be find. I really don’t know what’s going on.