Using Selenium with chromedriver

Hi there,

I have a python script that uses Selenium and Chrome Webdriver to log in to my bank website to scrape the account balances.

It’s working as expected in windows, I’d now like to run it from within Home Assistant once or twice daily, and have the values update relevant HA sensors.

I was thinking that Appdaemon would be the simplest way to run the script, so I installed the Appdaemon addon and configured Selenium package to be installed. But I can’t figure out how/where to install Chromedriver and how to specify its location.

My system details:

 Appdaemon add-on version: 0.3.2
 System: Ubuntu 18.04.5 LTS  (amd64 / qemux86-64) on Hyper V
 Home Assistant Core: 2020.12.1
 Home Assistant Supervisor: 2020.12.7

The errors I see in the Appdaemon log when loading my app.py:

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'

and

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

Could somebody please point me to an example where Selenium & webdriver is being used with Home Assistant, as my searches have not fared so well?
The other option I was thinking was to run the python app on another VM, outside of Home Assistant, with the values being sent via MQTT. But this feels like a bit of a waste of resources.

cheers, Anthony

Did you get this sorted out?

Sorry I just saw your message now. I ended up going the route of a separate VM to run the python/selenium code, which then sends the value to HA via MQTT. Crude but it works.

Thanks - I landed on the same.

I successfully run a python script with Selenium and Chrome Webdriver in Appdaemon of Hassio for 5 hour debuging.
Here are the key setting:
the setting in AppDaemon4 of Hassio:

system_packages:
  - chromium-chromedriver
  - chromium
python_packages:
  - selenium

the code in python:

        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')
        chrome_options.add_argument('--disable-dev-shm-usage')
        driver = webdriver.Chrome('chromedriver', options=chrome_options)

good luck!

1 Like

I run selenium with apps in Appdaemon. This is the helper file I import into appdaemon apps to help me run scripts. I’m still working on it so it is a mess ,but it works.

For selenium I run it as a docker image in hub mode using this image.

You can install the selenium driver by adding selenium to your requirements.txt file.

I run my hassio in a VM on a win10 pc, is there a way to make this work? I presently scrape using Excel and VBA so wanted to know if I can mimic this and have sensors within Hassio record my scraping.

This is my py code:

import logging
import time
CONF_ID = "id"
CONF_ACCOUNT_KEY = "account_key"
CONF_NAME_OVERRIDE = "name_override"

SENSOR_PREFIX = "Kadenabalance "

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')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', options=chrome_options)

driver.get("https://balance.chainweb.com/index.html")
time.sleep(5)
driver.find_element_by_id("account").SendKeys (CONF_ACCOUNT_KEY)
driver.find_element_by_class_name("button").Click
time.sleep(5)
ATTR_BALANCE = driver.find_element_by_id("chain-0-balance-data").Text

_LOGGER = logging.getLogger(__name__)

I keep getting: Platform error sensor.kadenabalance - No module named 'selenium’

I added this to AppDaemon 4:

init_commands: []
python_packages:
  - selenium
system_packages:
  - chromium-chromedriver
  - chromium