[Selenium]EdgeのIEモードで新しいタブがブロックされる
解決したいこと
SeleniumでEdgeのIeモードを動かしています。
新しいタブを開きたいのですが、ポップアップブロッカーによって開けません。
解決方法を教えて下さい。
発生している問題・エラー
新しいタブを開こうとすると、URLの右側に「ポップアップがブロックされました」と表示され、タブが開けません。
該当するソースコード
from selenium import webdriver
from selenium.webdriver.chrome import service as fs
import time
from winreg import *
ie_options = webdriver.IeOptions()
ie_options.attach_to_edge_chrome = True
ie_options.ignore_zoom_level = True
ie_options.edge_executable_path = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
#レジストリのPopupMgr値を変更
def set_popupblocker_status(enabled):
key = OpenKey(HKEY_CURRENT_USER, r"Software\Microsoft\Internet Explorer\New Windows", 0, KEY_ALL_ACCESS)
SetValueEx(key, "PopupMgr", 0, REG_SZ, enabled)
CloseKey(key)
try:
#レジストリ変更
set_popupblocker_status("no")
#ブラウザを開く(EdgeのIeモード)
ie_service = fs.Service(executable_path="IE driverがあるパス")
driver = webdriver.Ie(service=ie_service,options=ie_options)
#googleトップページに移動
driver.get("https://www.google.com/webhp?hl=ja&sa=X&ved=0ahUKEwjn7YH9zez0AhV3s1YBHR7lDX0QPAgI&safe=active&ssui=on")
#新しいタブでyahooトップ画面を開く
driver.execute_script("window.open('https://www.yahoo.co.jp/');")
time.sleep(5)
finally:
driver.quit()
print("finished")
自分で試したこと
①レジストリのPopupMgr値を変更
↓こちらの記事を参考に、レジストリの値を変更してポップアップブロックを無効にする方法を試しましたが、やはりブロックされてしまいます。
②driver.get()を行わない
試行錯誤の結果、新しいタブを開く前に一回でもdriver.get()を行うと本事象が発生することが分かりました。試しにdriver.get()の部分をコメントアウトして実行すると、ポップアップブロックはされず、新しいタブを開くことができました。
from selenium import webdriver
from selenium.webdriver.chrome import service as fs
import time
from winreg import *
ie_options = webdriver.IeOptions()
ie_options.attach_to_edge_chrome = True
ie_options.ignore_zoom_level = True
ie_options.edge_executable_path = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
#レジストリのPopupMgr値を変更
def set_popupblocker_status(enabled):
key = OpenKey(HKEY_CURRENT_USER, r"Software\Microsoft\Internet Explorer\New Windows", 0, KEY_ALL_ACCESS)
SetValueEx(key, "PopupMgr", 0, REG_SZ, enabled)
CloseKey(key)
try:
#レジストリ変更
set_popupblocker_status("no")
#ブラウザを開く(EdgeのIeモード)
ie_service = fs.Service(executable_path="IE driverがあるパス")
driver = webdriver.Ie(service=ie_service,options=ie_options)
#googleトップページに移動 ←を行わないと新しいタブが開ける
#driver.get("https://www.google.com/webhp?hl=ja&sa=X&ved=0ahUKEwjn7YH9zez0AhV3s1YBHR7lDX0QPAgI&safe=active&ssui=on")
#新しいタブでyahooトップ画面を開く
driver.execute_script("window.open('https://www.yahoo.co.jp/');")
time.sleep(5)
finally:
driver.quit()
print("finished")
しかしdriver.get()を行わないという縛りは避けたいため、この方法は使えません。
どなたか解決策のご教授の程宜しくお願い致します。
0 likes