Python保护设置IE

2024-05-18 05:52:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在IE using Selenium with python中遇到自动设置保护的问题。

我找到了一个在java中自动设置的解决方案,但是当我将其更改为python时,它不起作用。

我尝试了以下方法:

from selenium import webdriver

caps=webdriver.DesiredCapabilites.INTERNETEXPLORER
caps['INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS']=True
driver=webdriver.Ie(caps)

这就给出了一个关于论点的错误。

当我使用driver = webdriver.Ie()时 它说所有区域的保护模式设置必须相同。

有谁能帮我在python中使用selenium自动化这件事。


Tags: 方法fromimportdriverseleniumwithjavacaps
3条回答

如果功能模式不起作用,则有另一种选择。

from selenium import webdriver
from selenium.webdriver.ie.options import Options

ie_options = Options()
ie_options.ignore_protected_mode_settings = True
driver = webdriver.Ie(options=ie_options)
driver.get('http://www.google.com')

根据documentation,在python selenum中,应该使用名为 ignoreProtectedModeSettings

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.INTERNETEXPLORER
caps['ignoreProtectedModeSettings'] = True

driver = webdriver.Ie(capabilities=caps)

所需的功能在某些情况下不起作用。下面是一个使用winreg从注册表更改保护设置的方法。

from winreg import *

    def Enable_Protected_Mode():
        # SECURITY ZONES ARE AS FOLLOWS:
        # 0 is the Local Machine zone
        # 1 is the Intranet zone
        # 2 is the Trusted Sites zone
        # 3 is the Internet zone
        # 4 is the Restricted Sites zone
        # CHANGING THE SUBKEY VALUE "2500" TO DWORD 0 ENABLES PROTECTED MODE FOR THAT ZONE.
        # IN THE CODE BELOW THAT VALUE IS WITHIN THE "SetValueEx" FUNCTION AT THE END AFTER "REG_DWORD".
        #os.system("taskkill /F /IM iexplore.exe")
        try:
            keyVal = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
            key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
            SetValueEx(key, "2500", 0, REG_DWORD, 0)
            print("enabled protected mode")
        except Exception:
            print("failed to enable protected mode")

相关问题 更多 >