【问题标题】:How to open usual chrome or Firefox window while using Selenium Webdriver?使用 Selenium Webdriver 时如何打开常用的 chrome 或 Firefox 窗口?
【发布时间】:2023-12-30 21:38:01
【问题描述】:

当您使用 Selenium 启动 Web 驱动程序时,它会打开一个新的、非常新鲜的相应 Web 浏览器实例(看起来好像刚刚安装,没有历史记录和默认设置)。

有什么方法可以打开通常的窗口,这些窗口将具有我在我的 chrome 或 Firefox 中完成的自定义设置,如附加组件和所有?

【问题讨论】:

    标签: java c# selenium selenium-webdriver


    【解决方案1】:

    您可以在 FireFox 中使用现有配置文件

    File profileDir = new File("Path to default profile")
    FirefoxProfile firefoxProfile = new FirefoxProfile(profileDir);    
    WebDriver webDriver = new FirefoxDriver(firefoxProfile);
    

    对于 Chrome,您可以使用以下选项:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
    driver = new ChromeDriver(options);
    

    【讨论】:

    • 但在这种情况下,您实际上是在向新实例添加设置。如果我们只能打开旧的常用窗户,我很感兴趣。假设有一些 Intranet 链接无法在新实例上打开(出于安全目的),但在通常的浏览器窗口上可以正常工作。
    • @Manish 更新了答案。请检查
    • 谢谢。 File.But FirefoxProfile firefoxProfile = new FirefoxProfile(@"C:\Users\mkh6kor\AppData\Roaming\Mozilla\Firefox\Profiles\rwyq8vbx.default");像魅力一样工作。
    • 知道如何在 python 中做同样的事情吗? @nitinchawda
    最近更新 更多