end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

Seleniumを使ってChromium版EdgeのIEモードを動かしてみた - python版

Seleniumを使ってChromium版EdgeのIEモードを動かしてみた - Qiita

上記urlを selenium for python版。

何となく動作します

#!python
# -*- coding: utf-8 -*-
import getopt
import os
import re
import sys
import time
from selenium import webdriver
from selenium.webdriver.ie.options  import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support     import expected_conditions
from selenium.webdriver.support.ui  import WebDriverWait

CONF = {
    "edge":{
        # download from
        # https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads
        'web_driver':"c:/Users/end0t/tmp/IE11_to_EDGE/msedgedriver.exe",
        'window_size':[1300,1100],
    },
    "ie": {
        # download from https://selenium-release.storage.googleapis.com/index.html
        'web_driver':"c:/Users/end0t/tmp/IE11_to_EDGE/IEDriverServer_Win32_3.150.1.exe",
        # 2021/8時点で最新のver.3.150.2では、find_elements_~()が動作しない
        #'web_driver':"c:/Users/end0t/tmp/IE11_to_EDGE/IEDriverServer_Win32_3.150.2.exe",
        'msedge_cmd':"c:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
        'window_size':[1300,1100],
    },
    'sleep'          : 2, # sec
    "implicitly_wait": 2, # sec
    'out_dir'        : "out",
    }

SCREEN_CAPTURE_URLS = [
    "https://www.yahoo.co.jp/",
    "https://www.google.co.jp/" ]


def main():
    # selenium によりbrowser起動.
    # 対象ブラウザに応じ、以下をcomment解除
    #browser = init_browser_edge()
    #browser = init_browser_edge_ie()
    browser = init_browser_ie()
    
    browser.implicitly_wait(CONF["implicitly_wait"])

    test_manipulate_elements(browser)   # test対象画面を操作
    test_screen_captures(browser)       # test対象urlを連続画面キャプチャ

    browser.close()                     # seleniumの終了処理
    browser.quit()


def my_sleep(*sec):
    if len(sec):
        time.sleep( sec[0] )
        return
    time.sleep( CONF["sleep"] )

def my_browser_wait(browser):
    try:
        WebDriverWait(browser, CONF["implicitly_wait"]).until(
            expected_conditions.presence_of_all_elements_located)
        return True
    except Exception as e:
        print(e)
        return False

    
def test_manipulate_elements(browser):

    browser.get("https://www.yahoo.co.jp/")
    my_browser_wait(browser)

    input_elms = browser.find_elements_by_css_selector("input[type=search]")
    print( input_elms )
    input_elms[0].send_keys("今日の天気")
    
    # input_elms[0].send_keys(Keys.ENTER)

    # 画面表示の倍率によっては
    # find_elements_by_css_selector()できても、click()できない気がします。
    btn_elms = browser.find_elements_by_css_selector("button[type=submit]")
    print( btn_elms )
    btn_elms[0].click()
    
    my_browser_wait(browser)
    
    
def test_screen_captures(browser):

    for test_url in SCREEN_CAPTURE_URLS:
        browser.get( test_url )
        my_browser_wait(browser)

        # 画面キャプチャを取得 & file保存
        save_img_path = calc_img_path( test_url )
        print( "save screen capture to "+save_img_path )
        browser.save_screenshot( save_img_path )

    
def calc_img_path(req_url):
    img_path = req_url
    img_path = re.sub('https?://', '',  img_path)
    img_path = re.sub('[\?/:.]',   '_', img_path)
    return CONF["out_dir"] + "/" + img_path + ".png"


def init_browser_edge_ie():
    # 以下により、内部的に msedge.exe --ie-mode-force が実行
    opts = Options()
    opts._options["ie.edgechromium"] = True
    opts._options["ie.edgepath"] = CONF["ie"]['msedge_cmd']
    opts._options["ignoreProtectedModeSettings"] = True

    browser = webdriver.Ie(CONF["ie"]['web_driver'], options=opts)
    browser.set_window_size(CONF["ie"]["window_size"][0],
                            CONF["ie"]["window_size"][1])
    return browser

def init_browser_ie():
    browser = webdriver.Ie(CONF["ie"]['web_driver'])
    browser.set_window_size(CONF["ie"]["window_size"][0],
                            CONF["ie"]["window_size"][1])
    return browser

def init_browser_edge():
    browser = webdriver.Edge(CONF["edge"]['web_driver'])
    browser.set_window_size(CONF["edge"]["window_size"][0],
                            CONF["edge"]["window_size"][1])
    return browser
    

if __name__ == '__main__':
    main()