【问题标题】:Run Selenium Chrome WebDriver on Azure Cloud Service?在 Azure 云服务上运行 Selenium Chrome WebDriver?
【发布时间】:2018-08-22 01:55:45
【问题描述】:

我有:ASP.NET Core2 App + Selenium 可以通过浏览器自动执行一些操作。

它在本地完美运行。使用所有 nuget 和 exe 的最新版本。

部署到 Azure 后创建 Webdriver 出现问题。

我试过了:

  • 将 .exe 文件包含到文件夹中并像这样使用它:

new ChromeDriver(ChromeDriverService.CreateDefaultService("./CORE/ExeFiles"), chromeOptions);

  • 使用独立 RemoteWebServer 运行作业:无法连接到它 + 作业在 Start-Stop 站点后消失。
  • 将 .exe 文件作为服务运行 - 死路一条;
  • 使用代码从 CMD 运行 .exe 文件:RemoteWebServer on 4444 port OK,但我无法连接到它。

了解一些防火墙或防病毒阻止内容,但找不到在 Azure 上配置必要属性的位置。

如何在 Azure 上使用 Selenium? 一些最简单的例子??,我正在为此奋斗 3 天 =(

附:也可以找到这篇文章https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks 最后是这个:

不受支持: PhantomJS/Selenium:尝试连接本地地址,也使用 GDI+。

替代方案?如何在 Azure 上使用 Selenium?

【问题讨论】:

    标签: c# asp.net azure selenium selenium-webdriver


    【解决方案1】:

    好消息!

    我得到了以下工作

    Azure Web 作业Azure 应用服务(S1 应用服务计划) + Selenium C# + 无浏览器中的网站上托管。 io 用于运行远程无头 chrome。

    确保只安装以下 2 个 Nuget 包中的 2 个 -

    1. Selenium.WebDriver
    2. Selenium.Support

    就是这样。

    我在安装 ChromeDriver 包时也遇到了问题,然后 ChromeDriver.exe 是 Azure 问题的原因。所以远离在 Azure 上运行浏览器。只需将其视为远程运行浏览器即服务的控制器。

    【讨论】:

    • 你有它的示例代码吗?如果没有虚拟机,无头 chrome 会在哪里运行?您的网络作业是否执行包含硒测试的控制台 exe?
    • Browserless.io 用于运行远程无头 chrome - 这是 browserless.io 提供的一项单独服务,不位于您的 webjob 或 VM 中 - 可以将其想象为从您的 webjob 发出的 web 服务调用。我使用一个执行 exe 的 webjob,它为远程浏览器运行 selenium 代码(在 browserless.io 上) - 请参阅下面评论中的示例代码
    【解决方案2】:

    它不适用于应用服务,您已经找到了解释它的限制和限制页面。

    话虽如此,它在云服务(带有角色)上工作得很好,是的good ol' Cloud Services

    WebRole 示例 —

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web.Http;
    using OpenQA.Selenium;
    using OpenQA.Selenium.PhantomJS;
    using OpenQA.Selenium.Support.UI;
    
    namespace WebRole1.Controllers
    {
        public class PhantomController : ApiController
        {
            /// <summary>
            /// Run PhantomJS UI tests against the specified URL
            /// </summary>
            /// <param name="URL">URL to test</param>
            public string Get(string URL)
            {
                string result = UITests.Test(URL);
                return result;
            }
        }
    
        /// <summary>
        /// UITests class
        /// </summary>
        public class UITests
        {
            /// <summary>
            /// Test implementation
            /// </summary>
            /// <param name="URL">URL to test</param>
            /// <returns></returns>
            public static string Test(string URL)
            {
                // Initialize the Chrome Driver
                // Place phantomjs.exe driver in the project root,
                // meaning same folder as WebRole.cs
                using (var driver = new PhantomJSDriver())
                {
                    try
                    {
                        // Go to the home page
                        driver.Navigate().GoToUrl(URL);
    
                        IWebElement input;
                        WebDriverWait wait = new WebDriverWait(
                            driver, TimeSpan.FromSeconds(2));
    
                        Func<IWebDriver, IWebElement> _emailInputIsVisible =
                            ExpectedConditions.ElementIsVisible(By.Id("email"));
                        wait.Until(_emailInputIsVisible);
                        input = driver.FindElementById("email");
                        input.SendKeys("imposter@mailinator.com");
                        driver.FindElementById("submit").Click();
                        var alertbox = driver.FindElementById("alert");
                        if (alertbox.Text.Contains("disposable"))
                        {
                            return "PASS";
                        }
                        else
                        {
                            return "FAIL: alertbox.Text should contain " + 
                                "the word 'disposable'";
                        }
                    }
    
                    catch (Exception ex)
                    {
                        return $"FAIL: {ex.Message}";
                    }
                }
            }
        }
    }
    

    或者,您可以查看Azure Container InstancesHeadless Chrome。有一个.NET SDK as well

    【讨论】:

    • 作为解决方案 - 我创建了虚拟机并在 IIS 上设置了站点。
    【解决方案3】:
    string apikey = ConfigurationManager.AppSettings["BROWSERLESS_API_KEY"];
    ChromeOptions chromeOptions = new ChromeOptions();
    // Set launch args similar to puppeteer's for best performance
    chromeOptions.AddArgument("--disable-background-timer-throttling");
    chromeOptions.AddArgument("--disable-backgrounding-occluded-windows");
    chromeOptions.AddArgument("--disable-breakpad");
    chromeOptions.AddArgument("--disable-component-extensions-with-background-pages");
    chromeOptions.AddArgument("--disable-dev-shm-usage");
    chromeOptions.AddArgument("--disable-extensions");
    chromeOptions.AddArgument("--disable-features=TranslateUI,BlinkGenPropertyTrees");
    chromeOptions.AddArgument("--disable-ipc-flooding-protection");
    chromeOptions.AddArgument("--disable-renderer-backgrounding");
    chromeOptions.AddArgument("--enable-features=NetworkService,NetworkServiceInProcess");
    chromeOptions.AddArgument("--force-color-profile=srgb");
    chromeOptions.AddArgument("--hide-scrollbars");
    chromeOptions.AddArgument("--metrics-recording-only");
    chromeOptions.AddArgument("--mute-audio");
    chromeOptions.AddArgument("--headless");
    chromeOptions.AddArgument("--no-sandbox");
    chromeOptions.AddAdditionalCapability("browserless.token", apikey, true);
    using (var driver = new RemoteWebDriver(new Uri("https://chrome.browserless.io/webdriver"), chromeOptions.ToCapabilities()))
    {
    //Your selenium code
    }
    

    【讨论】:

    【解决方案4】:

    我遇到了同样的错误,我尝试了这个,它对我有用。 我在 repo 本身中添加了 chromedriver.exe,并在代码中给出了相对路径。它工作正常,因为它在本地环境中工作。

    Chromedriver

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2017-09-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2019-05-08
      • 2014-07-12
      相关资源
      最近更新 更多