在Azure云服务上运行Selenium Chrome WebDriver?

时间:2018-08-22 01:55:45

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

我有: ASP.NET Core2应用 + ,可通过浏览器自动执行某些操作。

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

部署到Azure后,在创建Webdriver时遇到问题。

我尝试过:

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

新的ChromeDriver(ChromeDriverService.CreateDefaultService(“ ./ CORE / ExeFiles”),chromeOptions);

  • 使用独立的RemoteWebServer运行作业:无法连接到它+作业在Start-Stop站点后消失。
  • 将.exe文件作为服务运行-死胡同;
  • 从CMD运行.exe文件,代码为:4444端口OK上的RemoteWebServer OK ,但是我无法连接到它。

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

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

P.S。另请参阅本文https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks 最后是这样:

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

替代品?如何在Azure上使用Selenium?

4 个答案:

答案 0 :(得分:2)

它不适用于App Service,并且您已经找到了说明它的限制和限制页面。

话虽这么说,它在带角色的Cloud Service上工作正常,是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

答案 1 :(得分:1)

好消息!

我有以下工作

Azure Web作业托管在 Azure App Service(S1 App Service计划) + Selenium C# + 无浏览器中。 io 用于运行远程无头chrome。

确保仅安装以下2个Nuget软件包中的2个-

  1. Selenium.WebDriver
  2. Selenium.Support

就是这样。

我还安装了ChromeDriver程序包时遇到了问题,然后ChromeDriver.exe成为了Azure上问题的原因。因此,不要在Azure上运行浏览器。只需将其视为可将浏览器作为服务远程运行的控制器即可。

答案 2 :(得分:0)

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
}

答案 3 :(得分:-2)

我遇到了同样的错误,我尝试了此操作,它为我工作。 我在存储库中添加了chromedriver.exe,并在代码中给出了相对路径。它可以在本地环境中正常工作。

Chromedriver