#!/usr/bin/env python # Copyright 2017 loblab # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from selenium import webdriver from selenium.common.exceptions import TimeoutException from pyvirtualdisplay import Display from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time import sys import os class Robot: USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0" USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1" LOGIN_URL = "https://127.0.0.1/index.php" WIDGET_URL = "https://127.0.0.1/index.php?v=d&p=dashboard&object_id=27" def __init__(self, debug=0): self.debug = debug options = webdriver.ChromeOptions() display = Display(visible=0, size=(1200, 800)) display.start() options.add_argument('--no-sandbox') options.add_argument("user-agent=%s" % Robot.USER_AGENT) self.browser = webdriver.Chrome(chrome_options=options) self.browser.set_page_load_timeout(60) def log_msg(self, msg, level=None): tstr = time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(time.time())) if level is None: level = self.debug if level > 0: print("%s [%s] - %s" % (tstr, self.username, msg)) def login(self, username, password): self.log_msg("Open %s..." % Robot.LOGIN_URL) self.browser.get(Robot.LOGIN_URL) if self.debug > 1: self.browser.save_screenshot("meteo1.png") self.log_msg("Login...") ele_usr = self.browser.find_element_by_id("in_login_username") ele_pwd = self.browser.find_element_by_id("in_login_password") ele_usr.send_keys(username) ele_pwd.send_keys(password) self.browser.find_element_by_id("bt_login_validate").click() self.browser.save_screenshot("meteo2.png") self.log_msg("Login OK...") def renderimg(self): self.log_msg("Open %s..." % Robot.WIDGET_URL) self.browser.get(Robot.WIDGET_URL) if self.debug > 1: self.browser.save_screenshot("meteo3.png") #beurk on double au cas ou self.browser.find_element_by_id("bt_meteoimg").click() self.browser.find_element_by_id("bt_meteoimg").click() self.log_msg("Click OK...") def run(self, username, password): rc = 0 self.username = username self.log_msg("Debug level: %d" % self.debug) try: self.login(username, password) if not self.renderimg(): rc = 3 except Exception as e: self.log_msg(str(e), 2) self.browser.save_screenshot("exception.png") rc = 2 finally: self.browser.quit() return rc def main(argv=None): if argv is None: argv = sys.argv if len(argv) < 2: print("Usage: %s []" % argv[0]) return 1 username = argv[1] password = argv[2] debug = 1 if len(argv) > 2: debug = int(argv[3]) robot = Robot(debug) return robot.run(username, password) if __name__ == "__main__": sys.exit(main())