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

6

我有一个ASP.NET Core2应用程序Selenium来自动化浏览器上的一些操作。

在本地工作正常。使用所有nuget和exe的最新版本。

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

我尝试过:

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

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

  • 使用独立RemoteWebServer运行Job:无法连接到它+启动站点后作业消失。
  • 将.exe文件作为服务运行-死路一条;
  • 从CMD运行.exe文件,代码为:远程Web服务器在4444端口上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个回答

4

好消息!我已经让以下内容运转起来

Azure Web Job托管在Azure App Service (S1应用服务计划)中,使用Selenium C#Browserless.io来运行远程无头浏览器。

确保只安装以下2个Nuget程序包-

  1. Selenium.WebDriver
  2. Selenium.Support

就是这样。

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


2
你有这方面的样例代码吗?没有虚拟机的情况下,无界面Chrome在哪里运行?你的WebJob是否执行包含Selenium测试的控制台可执行文件? - int-i
Browserless.io是用于运行远程无头Chrome的服务,它不包含在您的WebJob或VM中 - 可以将其视为从WebJob发出的Web服务调用。我使用一个WebJob来执行一个exe文件,该文件运行Selenium代码以远程控制浏览器(在browserless.io上)- 请参见下面评论中的示例代码。 - Sangeet

2

这不适用于 App Service,你已经找到了解释它的限制和局限的页面。

话虽如此,它在云服务(具有角色)上运行得非常好,是的,就是这个好老的云服务

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容器实例无界面Chrome。同时还有一个.NET SDK


作为解决方案 - 我创建了虚拟机并在IIS上设置了网站。 - user2377299

1
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
}

这非常有帮助。谢谢。 - int-i
这段关于无浏览器的内容,在此文档中也有描述:https://docs.browserless.io/docs/dotnet.html - Kenneth Bo Christensen

-3

我曾经遇到同样的错误,尝试了这个方法后问题得到了解决。 我将chromedriver.exe文件添加到代码库中,并在代码中使用相对路径。它在本地环境下运行良好。

Chromedriver


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接