简体   繁体   中英

Run Selenium Chrome WebDriver on Azure Cloud Service?

I have : ASP.NET Core2 App + Selenium to automate some actions with browser.

It works perfect on local.Use the latest versions of all nuget and exe.

After deploy to Azure have problems on create Webdriver.

I tried:

  • Include .exe files to folder and use it like :

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

  • Run Job with standalone RemoteWebServer: Cant connect to it + Job disappears after Start-Stop site.
  • Run .exe files as service - dead end;
  • Run .exe file from CMD with code: RemoteWebServer on 4444 port OK , but I can't connect to it.

Read about some Firewall or Antivirus blocking stuff but cant find where to configure necessary properties on Azure.

How can I use Selenium on Azure? Some simplest example pls??, I'm fighting with this for 3 days =(

PS Also find this article https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks and THIS in the end:

Unsuported: PhantomJS/Selenium: tries to connect to local address, and also uses GDI+.

Alternatives? How to use Selenium on Azure?

WELL, GOOD NEWS!!

I got the following working

Azure Web job hosted along a website in Azure App Service (S1 App Service plan) + Selenium C# + Browserless.io for running remote headless chrome.

Make sure you install only 2 the following 2 Nuget Packages -

  1. Selenium.WebDriver
  2. Selenium.Support

That's it.

I had issues when I installed ChromeDriver package also and then the ChromeDriver.exe was the cause of problems on Azure. So stay away from running the browser on Azure. Just think of it as the controller which runs the browser as service remotely.

It won't work on App Service and you already found the limits and limitations page that explains it.

That being said, it works just fine on a Cloud Service (with Roles), yes the good ol' Cloud Services .

WebRole sample —

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}";
                }
            }
        }
    }
}

Alternatively you can look atAzure Container Instances with Headless Chrome . There's a .NET SDK as well .

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
}

I was facing the same errors and I tried this and Its working for me. I added the chromedriver.exe in the repo itself and gave relative path in the code. It is working fine as it works in local env.

Chromedriver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM