跳到主內容

【Python】selenium自動播放flash

python selenium firefox 控制devtools 一些线索_wujiuqier的博客-CSDN博客


軟體載點

後來測試可用的組合為

python 3.7.0
firefox 57.0.4
geckodriver 0.19.1
selenium 3.141.0

版本55或以上的Firefox不支持Flash自动播放。
建议使用Firefox 52.9.0 延长支持版。
此版本需要使用Gecko Driver 0.18.0才能正常使用。但是不支持 set_window_size(会报错selenium.common.exceptions.WebDriverException: Message: setWindowRect),所以必须手动调整视口尺寸。 应该使用find_element_by_id(element_id).screenshot(output_file_name)的方法来截长图,或者也可以用find_elements_by_tag_name('body')[0].screenshot(output_file_name)的方法来截整页长图。
自动播放Flash的设置(参考https://blog.csdn.net/STL_CC/article/details/104968669):


此版本还没有移除GCLI开发者工具栏,可以通过快捷键 Shift + F2呼出。
调整视口尺寸的GCLI命令是resize to 4320 7680。第一个参数是宽,第二个参数是高。
截图的命令是

既然不能控制视口,那就直接控制外层div的样式,然后执行元素节点截图。直接用execute_script即可。
综上,截取小花仙人物面板形象的操作有:

image-1665654519510.png


from time import sleep
from selenium import webdriver
import win32api
import win32gui
import win32con
import os
import requests
import sys
import time
from PIL import ImageGrab
import win32com.client

login_info = [
    {
        "file_name": "db1.jpeg",
        "url": "https://1.2.3.4/em/console/database/instance/waitDetails?event=doLoad&target=mall&type=oracle_database&waitClass=Overview&datasource=SQL",
        "username": "aa",
        "title_id": None,
        "password": "aaaaaaaa",
        "hwnd": None,
        "driver": None},
    {
        "file_name": "db2.jpeg",
        "url": "https://1.2.3.4/em/faces/sdk/nonFacesWrapper?target=reportdb&_em.coBM=%2Fconsole%2Fdatabase%2Finstance%2FwaitDetails%3Fevent%26target%3Dreportdb%26type%3Doracle_database%26waitClass%3DOverview%26tabName%3Doverview%26selectedBand%3D1%26leftEdge%3Dundefined&type=oracle_database",
        "username": "bb",
        "password": "bbbbbbbb",
        "title_id": None,
        "j_username": "cc",
        "j_password": "cccccccc",
        "hwnd": None,
        "driver": None}
]


def api_upload(filename):
    url = "http://192.168.1.1/api/v1/db/uploadIMG.php"
    local_folder_path = getRootPath()  # 取得目前執行檔目錄
    image = local_folder_path + "\\" + filename
    files = {"file": (filename, open(image, "rb"), "rb")}
    response = requests.request("POST", url, data=None, files=files)
    # print(response.text)


def getRootPath():
    return os.path.dirname(os.path.abspath(__file__))


def main():
    for config in login_info:
        try:
            profile = webdriver.FirefoxProfile()
            profile.set_preference('plugin.state.flash', 2)
            profile.set_preference('security.tls.version.min', 1)
            profile.set_preference('security.insecure_field_warning.contextual.enabled', False)
            if config["driver"] == None:
                driver = webdriver.Firefox(executable_path=r'C:\screenshot\geckodriver.exe', firefox_profile=profile)
                config["driver"] = driver
                driver.get(config["url"])

                if config["file_name"] == 'mallDB.jpeg':
                    username_input = driver.find_element_by_id("M__Id")
                    username_input.send_keys(config["username"])
                    password_input = driver.find_element_by_id("M__Ida")
                    password_input.send_keys(config["password"])
                    driver.find_element_by_xpath('//table/tbody/tr[4]/td[3]/a/img').click()
                    sleep(7)
                    config["title_id"] = win32gui.GetForegroundWindow()
                elif config["file_name"] == 'reportDB.jpeg' or config["file_name"] == 'wmsDB.jpeg' or config["file_name"] == 'scmDB.jpeg':
                    j_username_input = driver.find_element_by_id("j_username::content")
                    j_username_input.send_keys(config["j_username"])
                    j_password_input = driver.find_element_by_id("j_password::content")
                    j_password_input.send_keys(config["j_password"])
                    login_btn = driver.find_element_by_id("login")
                    login_btn.click()
                    sleep(5)
                    username_input2 = driver.find_element_by_id("emT:r1:0:r1:0:username::content")
                    username_input2.send_keys(config["username"])
                    password_input2 = driver.find_element_by_id("emT:r1:0:r1:0:password::content")
                    password_input2.send_keys(config["password"])
                    login_btn_emT = driver.find_element_by_id("emT:logon")
                    login_btn_emT.click()
                    sleep(5)
                    config["title_id"] = win32gui.GetForegroundWindow()

                title_id = win32gui.GetWindowText(config["title_id"])
                hwnd = win32gui.FindWindow(None, title_id)
                shell = win32com.client.Dispatch("WScript.Shell")
                shell.SendKeys('%')
                win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
            config["driver"].save_screenshot(config["file_name"])
            api_upload((config["file_name"]))
            sleep(3)

        except Exception as e:
            print("exception:", e)


if __name__ == "__main__":
    while True:
        main()