使用 Selenium 在私有模式下打开 Internet Explorer Python

Opening Internet Explorer in Private Mode with Selenium Python

我正在尝试在 python 中使用 selenium 在 IE 中打开 gmail,但是在第一次登录后,我仍然保持登录状态,这破坏了我的代码,我希望它以私密方式启动模式以确保我不会保持登录状态。这是我的代码:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class internet_explorer:
    def __init__(self):
        self.driver = webdriver.Ie(executable_path='venv\Scripts\IEDriverServer.exe')
        self.driver.get("https://gmail.com")
        time.sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"identifier\"]") \
            .send_keys(em + Keys.ENTER)
        time.sleep(1)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]") \
            .send_keys(pw + Keys.ENTER)
        time.sleep(10)
        self.driver.close()
        self.driver.quit()

internet_explorer()

不胜感激!

我建议您可以尝试参考下面的示例以隐私模式启动 IE 浏览器。

caps = DesiredCapabilities.INTERNETEXPLORER
caps["se:ieOptions"] = {}
caps["se:ieOptions"]['ie.forceCreateProcessApi'] = True
caps["se:ieOptions"]['ie.browserCommandLineSwitches'] ='-private'
caps["se:ieOptions"]["ie.ensureCleanSession"] = True
driver = webdriver.Ie(executable_path='C:\selenium\IEDriverServer_x64_3_8.exe', capabilities=caps)

如果问题仍然存在,请尝试提供有关该问题的详细信息,例如您使用的 IE 版本、Selenium 版本等。