Digital Signage: Python Selenium Script to Launch Chrome in Kiosk Mode

Python
2022-04-02 14:32 (3 years ago) ytyng

Script to Open a Website and Login to Google Using Python Selenium

#!/usr/bin/env bash
import os
import time

import dotenv
from selenium.webdriver import Chrome, ChromeOptions, DesiredCapabilities


def create_driver():
options = ChromeOptions()
arguments = [
'--lang=ja',
# '--window-size=1920,1080',
# '--window-position=0,0',
'--kiosk',
'--start-fullscreen',
'--noerrdialogs',
'--disable-translate',
'--disable-infobars',
'--disable-features=TranslateUI',
]
for arg in arguments:
options.add_argument(arg)
# Keep the browser running even after the Python script ends
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

# Settings to accept self-signed certificates
capabilities = DesiredCapabilities.CHROME
capabilities['acceptInsecureCerts'] = True
# capabilities['acceptSslCerts'] = True # This setting might be unnecessary

driver = Chrome(options=options, desired_capabilities=capabilities)
return driver


def main():
dotenv.load_dotenv()
driver = create_driver()
driver.get('https://www.example.com/')
time.sleep(2)
driver.find_element_by_xpath("//*[@id='identifierId']") \
.send_keys(os.environ.get('USER_EMAIL'))
driver.find_element_by_xpath("//*[@id='identifierNext']").click()
time.sleep(2)
driver.find_element_by_xpath(
"//*[@id='password']/div[1]/div/div[1]/input"
).send_keys(os.environ.get('USER_PASSWORD'))
driver.find_element_by_xpath("//*[@id='passwordNext']").click()


if __name__ == '__main__':
main()
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011