簡體   English   中英

Selenium C# chromedriver with DesiredCapabilities

[英]Selenium C# chromedriver with DesiredCapabilities

我想在 C# 中使用 chrome 驅動程序實例實現 DesiredCapabilities 但從我能發現它是不可能的? 例如...

DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(CapabilityType.UnexpectedAlertBehavior, "accept");               
Driver = new ChromeDriver(@"C:\Development\Projects\SeleniumObservatoryTests\", capabilities);

ChromeDriver 似乎需要選項。 有沒有辦法提供 UnexpectedAlertBehavior 作為選項?

我也無法讓它在 Chrome 上工作。 對於 Firefox,我必須使用 DesiredCapabilities,對於 IE,我必須使用 InternetExplorerOptions。

但是,我已經成功地使用 Chrome 處理彈出窗口,並編寫了一些代碼來處理它。

我的代碼貼在下面。 我希望它可以幫助您和其他受模態對話框困擾的人。

    /// <summary>
    /// Returns a true or false if alert is present.
    /// </summary>
    /// <returns>true or false</returns>
    public static bool IsAlertPresent()
    {
        try
        {
            [YOUR DRIVER HERE].SwitchTo().Alert();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    /// <summary>
    /// Verifies if an alert is present or not.  If it is, it clicks "Accept".
    /// </summary>
    public static void Alert()
    {
        try
        {
            if (IsAlertPresent())
                [YOUR DRIVER HERE].SwitchTo().Alert().Accept();

        }
        catch (Exception ex)
        {
            ///Log your errors however you must.
        }
    }

我知道這並不花哨或任何東西,但它完成了工作。 基本上它會嘗試切換到警報,如果在那里,則返回 true,否則返回 false。 如果為真,它會再次切換並單擊接受。 很簡單。 還沒有讓我失望。

您應該能夠添加具有以下功能的功能:

var options = new ChromeOptions();
options.AddAdditionalCapability(CapabilityType.UnexpectedAlertBehavior, "accept");

您也可以忽略警報:

var options = new ChromeOptions();

options.AddAdditionalCapability(CapabilityType.UnexpectedAlertBehavior, "ignore");

我相信這就是 WebDriver 4.x .NET 綁定的方式...

var options = new ChromeOptions();
Dictionary<string, object> browserStackOptions = new Dictionary<string, object>();
browserstackOptions.Add("unhandledPromptBehavior", "ignore");
options.AddAdditionalOption("bstack:options", browserStackOptions);
IWebDriver driver = new ChromeDriver(options);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM