How to prevent (python) Selenium webdriver from triggering “Microsoft Malicious Software Removal Tool” to ask if it may reset browser settings?

爱⌒轻易说出口 提交于 2021-02-10 06:39:08

问题


Recently Selenium chrome webdriver started to run into problems by triggering Microsoft Malicious Software Removal Tool to request if it may reset browser settings. How to get around this? Is there an argument to add to options like --disable-extensions solved a popup problem before?

    from selenium import webdriver
    options = webdriver.chrome.options.Options()
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options)

A temporary solution may be

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

but nothing happens.

What works (but obviously not ideal) is to execute javascript to close the undesired tab:

time.sleep(0.2) # give some time for the tabs to appear

# just to understand that the first tab is counter-intuitivly the last of window.handles change the content of each tap
js = "document.getElementsByTagName('body')[0].innerHTML = 'This is handle {0}: {1}';"
for i, handle in enumerate(driver.window_handles):
    driver.switch_to_window(handle)
    driver.execute_script(js.format(i,handle))

# now close the msrt tab and make the desired tab active    
handle_desired, handle_msrt = driver.window_handles # last handle is first tab
driver.switch_to_window(handle_msrt)
driver.execute_script('window.close()') # close the msrt tab
driver.switch_to_window(handle_desired)

来源:https://stackoverflow.com/questions/43368028/how-to-prevent-python-selenium-webdriver-from-triggering-microsoft-malicious

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!