【Power Automate Desktop】Webブラウザの操作を自動化する方法[#1]

Power Automate Desktop

こんにちはmOqOmです。
この記事では、前回【Power Automate Desktopのダウンロード・インストール方法】で紹介したPower Automate Desktopを用いてWebブラウザの自動化を行う方法を紹介しています。
まずは基本となる【 Power Automate Desktopのフロー作成 】と【 Webブラウザの起動と終了 】について紹介します。
また今回は、Webレコーダーを使用せずにアクションを追加する方法を紹介しています。

Webブラウザの自動化

Power Automate Desktopを用いたWebブラウザ操作の自動化を機能ごとに紹介していきます。
紹介した自動化処理を組み合わせることで、Power Automate Desktopを使用してWebブラウザの自動化で様々なことができるようになります。

※今回の説明では【 Microsoft Edge 】を用いて説明を行いますが、Microsoft Edgeを使用するブラウザ名に読み替えていただければ、Google ChromeやFirefoxでも同じことが行えます。

フローの作成

まず自動化を保存するフローを作成していきます。
Power Automate Desktopを起動し【 新しいフロー 】をクリックします。

【 フロー名 】に分かりやすい名前を付け【 作成 】をクリックします。
今回は【Webブラウザの起動と終了】と入力しています。

【 フロー名 】を付け【 作成 】をクリックすると別ウィンドウが開きます。
この開いた別ウィンドウで作業を行っていきます。
これでフローの作成は完了となります。次にアクションを追加して自動化してみましょう。

Webブラウザの起動と終了

まずは基礎となるWebブラウザの起動と終了のフローを作成していきます。

【 ブラウザー自動化 】から【 新しいMicrosoft Edgeを起動 】を【 ダブルクリック 】または【 Main 】に【 ドラッグアンドドロップ(D&D) 】をします。

【 新しいMicrosoft Edgeを起動 】が追加されるとパラメータが選択できるようになるで【 初期URL 】を入力し、【 保存 】をクリックします。

※今回は分かりやすいように【 ウィンドウの状態 】を【 最大化 】に設定しますがデフォルト状態でも問題ありません。

【 新しいMicrosoft Edgeを起動する 】を追加した時と同様に【 フローコントロール 】から【 Wait 】を追加します

【 Wait 】のパラメータに適当な数字(今回は3)を入力し、【 保存 】をクリックします。
※ここで入力するパラメータはWebブラウザが起動してから何秒待つかの設定になります。また単位は【 秒 】となります。

最後に【 ブラウザー自動化 】から【 Webブラウザを閉じる 】を追加します。

【 Webブラウザを閉じる 】のパラメータはデフォルトから変更せずに【 保存 】をクリックします。

このフローで【 Webブラウザの起動 】から【 Webブラウザの停止 】まで追加したので、実際に追加した通りに動くか試していきます。
下の画像の赤枠の再生ボタンをクリックすると「指定したWebブラウザでページが開き3秒後にWebブラウザが閉じらる」ことが確認できると思います。これでPower Automate Desktopを使用したWebブラウザの基本は終わりです。もし、指定した通りに動かない場合は、Webブラウザに拡張機能がインストールされているかなどを確認して再度同じ手順で実行してみてください。

今回の【 Webブラウザの起動と終了 】フローを手順通りに実行すると以下のような動作になります。

Webページのデータを取得

【 Webブラウザの起動と終了 】で使用したフローを再利用し、中央に挿入した【 Wait】を【 ブラウザー自動化 】の【 Webデータ抽出 】から【 Webページ上の要素の詳細を取得します 】に入れ替えます。

【 Webページ上の要素の詳細を取得します 】のパラメータの【 UI要素 】を選択し【 UI要素の追加 】をクリックします。

データを読み込みたいWebブラウザでページを開いておき、読み込みしたい項目をマウスカーソルで合わせると文字がついている赤枠(今回の場合は「Navigation」)で囲まれます。今回読み込みたい要素(Element)であることが分かったので、【 Ctrlキー 】を押しながら表示されている赤枠を左クリックします。

読み込みたい要素が【 追跡セッション 】に追加されたことを確認できたら【 完了 】をクリックします。
【 Webブラウザインターフェース 】はデフォルトのままにしておき【 UI要素 】が設定されていること、属性名が【 Own Text 】であることを確認して【 保存 】をクリックします。

最後に【 Webブラウザの起動と終了 】と同様に【 Webブラウザを閉じる 】を追加します。
【 Webブラウザを閉じる 】を追加できたら、想定通りに動くか試していきます。
正常にWebブラウザが閉じられ、データの取得ができれば【 AttributeValue 】に文字が出力されます。

今回の【 Webページのデータを取得 】フローを手順通りに実行すると以下のような動作になります。

さいごに

これでPower Automate Desktopを用いたWebブラウザ自動化はできたのではないでしょうか。もし「こんなことを自動化したいけど、どうフローを作成したらいいの?」などという場合はコメント欄にコメントをお願いします。私のできる範囲で回答したと思います。

コメント

");const o=ie?ie.createHTML(e):e;if(nt===tt)try{t=(new W).parseFromString(o,at)}catch(e){}if(!t||!t.documentElement){t=le.createDocument(nt,"template",null);try{t.documentElement.innerHTML=ot?ae:o}catch(e){}}const i=t.body||t.documentElement;return e&&n&&i.insertBefore(r.createTextNode(n),i.childNodes[0]||null),nt===tt?ue.call(t,Ie?"html":"body")[0]:Ie?t.documentElement:i},bt=function(e){return ce.call(e.ownerDocument||e,e,H.SHOW_ELEMENT|H.SHOW_COMMENT|H.SHOW_TEXT|H.SHOW_PROCESSING_INSTRUCTION|H.SHOW_CDATA_SECTION,null)},St=function(e){return e instanceof B&&("string"!=typeof e.nodeName||"string"!=typeof e.textContent||"function"!=typeof e.removeChild||!(e.attributes instanceof z)||"function"!=typeof e.removeAttribute||"function"!=typeof e.setAttribute||"string"!=typeof e.namespaceURI||"function"!=typeof e.insertBefore||"function"!=typeof e.hasChildNodes)},Rt=function(e){return"function"==typeof b&&e instanceof b},wt=function(e,t,n){pe[e]&&u(pe[e],(e=>{e.call(o,t,n,ut)}))},Ct=function(e){let t=null;if(wt("beforeSanitizeElements",e,null),St(e))return _t(e),!0;const n=st(e.nodeName);if(wt("uponSanitizeElement",e,{tagName:n,allowedTags:Ne}),e.hasChildNodes()&&!Rt(e.firstElementChild)&&_(/<[/\w]/g,e.innerHTML)&&_(/<[/\w]/g,e.textContent))return _t(e),!0;if(e.nodeType===J)return _t(e),!0;if(Me&&e.nodeType===Q&&_(/<[/\w]/g,e.data))return _t(e),!0;if(!Ne[n]||Ce[n]){if(!Ce[n]&&Dt(n)){if(we.tagNameCheck instanceof RegExp&&_(we.tagNameCheck,n))return!1;if(we.tagNameCheck instanceof Function&&we.tagNameCheck(n))return!1}if(Ye&&!qe[n]){const t=re(e)||e.parentNode,n=oe(e)||e.childNodes;if(n&&t){for(let o=n.length-1;o>=0;--o){const r=X(n[o],!0);r.__removalCount=(e.__removalCount||0)+1,t.insertBefore(r,$(e))}}}return _t(e),!0}return e instanceof R&&!Et(e)?(_t(e),!0):"noscript"!==n&&"noembed"!==n&&"noframes"!==n||!_(/<\/no(script|embed|frames)/i,e.innerHTML)?(ke&&e.nodeType===Z&&(t=e.textContent,u([fe,de,he],(e=>{t=g(t,e," ")})),e.textContent!==t&&(p(o.removed,{element:e.cloneNode()}),e.textContent=t)),wt("afterSanitizeElements",e,null),!1):(_t(e),!0)},Lt=function(e,t,n){if(Be&&("id"===t||"name"===t)&&(n in r||n in mt))return!1;if(ve&&!Le[t]&&_(ge,t));else if(De&&_(Te,t));else if(!Se[t]||Le[t]){if(!(Dt(e)&&(we.tagNameCheck instanceof RegExp&&_(we.tagNameCheck,e)||we.tagNameCheck instanceof Function&&we.tagNameCheck(e))&&(we.attributeNameCheck instanceof RegExp&&_(we.attributeNameCheck,t)||we.attributeNameCheck instanceof Function&&we.attributeNameCheck(t))||"is"===t&&we.allowCustomizedBuiltInElements&&(we.tagNameCheck instanceof RegExp&&_(we.tagNameCheck,n)||we.tagNameCheck instanceof Function&&we.tagNameCheck(n))))return!1}else if(Ze[t]);else if(_(Ae,g(n,Ee,"")));else if("src"!==t&&"xlink:href"!==t&&"href"!==t||"script"===e||0!==T(n,"data:")||!Ke[e]){if(Oe&&!_(ye,g(n,Ee,"")));else if(n)return!1}return!0},Dt=function(e){return"annotation-xml"!==e&&h(e,_e)},vt=function(e){wt("beforeSanitizeAttributes",e,null);const{attributes:t}=e;if(!t)return;const n={attrName:"",attrValue:"",keepAttr:!0,allowedAttributes:Se};let r=t.length;for(;r--;){const i=t[r],{name:a,namespaceURI:l,value:c}=i,s=st(a);let p="value"===a?c:y(c);if(n.attrName=s,n.attrValue=p,n.keepAttr=!0,n.forceKeepAttr=void 0,wt("uponSanitizeAttribute",e,n),p=n.attrValue,n.forceKeepAttr)continue;if(At(a,e),!n.keepAttr)continue;if(!xe&&_(/\/>/i,p)){At(a,e);continue}if(Me&&_(/((--!?|])>)|<\/(style|title)/i,p)){At(a,e);continue}ke&&u([fe,de,he],(e=>{p=g(p,e," ")}));const f=st(e.nodeName);if(Lt(f,s,p)){if(!We||"id"!==s&&"name"!==s||(At(a,e),p=Ge+p),ie&&"object"==typeof G&&"function"==typeof G.getAttributeType)if(l);else switch(G.getAttributeType(f,s)){case"TrustedHTML":p=ie.createHTML(p);break;case"TrustedScriptURL":p=ie.createScriptURL(p)}try{l?e.setAttributeNS(l,a,p):e.setAttribute(a,p),St(e)?_t(e):m(o.removed)}catch(e){}}}wt("afterSanitizeAttributes",e,null)},Ot=function e(t){let n=null;const o=bt(t);for(wt("beforeSanitizeShadowDOM",t,null);n=o.nextNode();)wt("uponSanitizeShadowNode",n,null),Ct(n)||(n.content instanceof s&&e(n.content),vt(n));wt("afterSanitizeShadowDOM",t,null)};return o.sanitize=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=null,r=null,i=null,l=null;if(ot=!e,ot&&(e="\x3c!--\x3e"),"string"!=typeof e&&!Rt(e)){if("function"!=typeof e.toString)throw A("toString is not a function");if("string"!=typeof(e=e.toString()))throw A("dirty is not a string, aborting")}if(!o.isSupported)return e;if(Ue||ft(t),o.removed=[],"string"==typeof e&&(je=!1),je){if(e.nodeName){const t=st(e.nodeName);if(!Ne[t]||Ce[t])throw A("root node is forbidden and cannot be sanitized in-place")}}else if(e instanceof b)n=Nt("\x3c!----\x3e"),r=n.ownerDocument.importNode(e,!0),r.nodeType===V&&"BODY"===r.nodeName||"HTML"===r.nodeName?n=r:n.appendChild(r);else{if(!Fe&&!ke&&!Ie&&-1===e.indexOf("<"))return ie&&ze?ie.createHTML(e):e;if(n=Nt(e),!n)return Fe?null:ze?ae:""}n&&Pe&&_t(n.firstChild);const c=bt(je?e:n);for(;i=c.nextNode();)Ct(i)||(i.content instanceof s&&Ot(i.content),vt(i));if(je)return e;if(Fe){if(He)for(l=se.call(n.ownerDocument);n.firstChild;)l.appendChild(n.firstChild);else l=n;return(Se.shadowroot||Se.shadowrootmode)&&(l=me.call(a,l,!0)),l}let m=Ie?n.outerHTML:n.innerHTML;return Ie&&Ne["!doctype"]&&n.ownerDocument&&n.ownerDocument.doctype&&n.ownerDocument.doctype.name&&_(q,n.ownerDocument.doctype.name)&&(m="\n"+m),ke&&u([fe,de,he],(e=>{m=g(m,e," ")})),ie&&ze?ie.createHTML(m):m},o.setConfig=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};ft(e),Ue=!0},o.clearConfig=function(){ut=null,Ue=!1},o.isValidAttribute=function(e,t,n){ut||ft({});const o=st(e),r=st(t);return Lt(o,r,n)},o.addHook=function(e,t){"function"==typeof t&&(pe[e]=pe[e]||[],p(pe[e],t))},o.removeHook=function(e){if(pe[e])return m(pe[e])},o.removeHooks=function(e){pe[e]&&(pe[e]=[])},o.removeAllHooks=function(){pe={}},o}();return oe}))
タイトルとURLをコピーしました