【问题标题】:Starting Edge Chromium in InPrivate mode / headless mode did not work for selenium在 InPrivate 模式/无头模式下启动 Edge Chromium 不适用于 selenium
【发布时间】:2020-09-08 00:53:10
【问题描述】:

我试图在我的 selenium / java 测试期间以私有模式启动边缘铬。我也想以无头模式启动它。我尝试了一些选项,但没有一个有效。

根据 Microsoft 网站,(https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium?tabs=c-sharp) 我将 UseChromium 属性作为 true 传递。但我仍然无法让无头和私人浏览工作。请提供您的想法。

Edge chromium 版本:版本 81.0.416.77(官方构建)(64 位) msedgedriver 版本:81.0.416.77(64 位) Selenium 版本:3.141.59(稳定版)

我的代码:

EdgeOptions options=new EdgeOptions();
options.setCapability("UseChromium", true);
options.setCapability("InPrivate", true);
driver = new EdgeDriver(options);

【问题讨论】:

    标签: selenium-webdriver microsoft-edge headless-browser


    【解决方案1】:

    我们可以添加参数inprivate 以使用 Selenium WebDriver 以私有模式打开 Edge Chromium。并且,添加参数 headlessdisable-gpu 以使用 Selenium WebDriver 以无头模式打开 Edge Chromium。

    请检查以下步骤:

    第一步

    here下载Selenium 4.00-alpha05的Java/C#绑定。

    this page下载匹配版本的Microsoft Edge Driver。

    第 2 步:

    使用 Selenium WebDriver 在私有模式下打开 Edge Chromium:

    Java 代码:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.edge.EdgeDriver;
    import org.openqa.selenium.edge.EdgeOptions;
    
    public class Edgeauto {
        public static void main(String[] args) { 
            System.setProperty("webdriver.edge.driver", "D:\\webdriver\\msedgedriver.exe"); 
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.addArguments("-inprivate");
            WebDriver driver = new EdgeDriver(edgeOptions); 
            driver.get("https://bing.com");
        }
    }
    

    C#代码:

    using OpenQA.Selenium.Edge;
    using System.Threading;
    
    namespace ecwebdriver
    {
        public class edgewebdriver
        {
            static void Main(string[] args)
            {
                EdgeOptions edgeOptions = new EdgeOptions();
                edgeOptions.UseChromium = true;
                edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
                edgeOptions.AddArgument("-inprivate");
                var msedgedriverDir = @"E:\webdriver";
                var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
                driver.Navigate().GoToUrl("https://bing.com");
                Thread.Sleep(3000);
                driver.Close();
            }
        }
    }
    

    使用 Selenium WebDriver 在无头模式下打开 Edge Chromium

    C#代码:

    using OpenQA.Selenium.Edge;
    using System.Threading;
    
    namespace ecwebdriver
    {
        public class edgewebdriver
        {
            static void Main(string[] args)
            {
                EdgeOptions edgeOptions = new EdgeOptions();
                edgeOptions.UseChromium = true;
                edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
                edgeOptions.AddArgument("headless");
                edgeOptions.AddArgument("disable-gpu");
    
                var msedgedriverDir = @"E:\webdriver";
                var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
                driver.Navigate().GoToUrl("<website url>");
                Thread.Sleep(3000);
                driver.Close();
            }
        }
    }
    

    注意:将代码中的webdriver路径和网址改成自己的。

    如果您想在私有模式和无头模式下启动边缘铬。 EdgeOptions 应如下所示:

            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.UseChromium = true; 
            edgeOptions.AddArgument("-inprivate");
            edgeOptions.AddArgument("headless");
            edgeOptions.AddArgument("disable-gpu")
    

    编辑

    如果使用 Selenium 3 版本,我们应该使用 NuGet CLI 或 Visual Studio 将 Microsoft.Edge.SeleniumToolsSelenium.WebDriver 包添加到我们的 .NET 项目中。更多详情请查看this link

    然后,添加以下引用:

    using Microsoft.Edge.SeleniumTools;
    

    并参考以下 C# 代码(记得将路径更改为您自己的):

            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.UseChromium = true;
            edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
            edgeOptions.AddArgument("-inprivate");
            //edgeOptions.AddArgument("headless");
            //edgeOptions.AddArgument("disable-gpu");
    
            var msedgedriverDir = @"E:\webdriver\edgedriver_win64_81_0_416_77\edgedriver_win64";
            var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
            driver.Navigate().GoToUrl("https://www.google.com");
            Thread.Sleep(3000);
            driver.Close();
    

    [注意] EdgeOptions 和 EdgeDriver 位于 Microsoft.Edge.SeleniumTools 命名空间中,而不是 OpenQA.Selenium.Edge 命名空间中。

    【讨论】:

    • 我使用的是稳定版的 selenium -> 3.141.59。有了这个,我无法从 edgeoptions 对象获取 addArguments() 方法。你能澄清一下相同的稳定版本吗?
    • @Sangeetha,请检查我的编辑,安装 Microsoft.Edge.SeleniumTools 包并使用它创建 EdgeOptions 和 EdgeDriver,然后您可以添加参数。
    • 由于我使用的是java,请您告诉java中的等效包名称吗?我们可以从哪里下载任何依赖项?
    • 对不起,据我所知,Selenium Tools 没有 Java 版本,在 Java 中使用 selenium 3 时,EdgeOptions 没有 AddArgument 方法。所以,我建议你可以下载 Selenium 4.00-alpha05 版本并使用它来添加参数。
    • @Gagan 不,还在同一页上。
    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2023-01-13
    • 2023-01-17
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多