Selenium - 无法连接到远程服务器

Selenium - Cannot Connect to Remote Server

让我先把错误告诉你,然后我会解释上下文,最后我会展示代码并解释它。

错误

这是使用 FirefoxDriver 的 same/similar 错误

上下文

我制作了一个程序来浏览网站并收集一些数据。这个 程序在我的本地桌面上 100% 工作 windows 7 professional 但是当我将它移动到我的服务器时,它是一个 windows 2003 服务器,带有 .net framework 3.5 它抛出上述错误。

请注意,应用程序是多线程的,在上面的例子中有两个线程 运行 2 个 selenium 实例。当他们收集完他们想要探索的 link 的列表时,应用程序就会出现问题。一个线程将一个一个地查找并遍历 links 列表,当另一个线程完成收集 links 时,它想探索两个 selenium 客户端中断并开始抛出上面的错误.

我没有使用任何不适用于 .net Framework 3.5 的功能....一切都已标准化以适应 2003 服务器(至少据我所知)。

代码

收集时links:

  List<string> totalList = new List<string>();
                if (loadedSave == null)
                {
                    webManager.driver.Navigate().GoToUrl(getOffenderListURL(countyId));
                    for (int l = 2; l < 10000; l++)
                    {
                        try
                        {
                            var element1 = new WebDriverWait(webManager.driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementExists((By.XPath(getOffenderxPath(l)))));

                            string linkToOffender = element1.GetAttribute("href");
                            string offenderId = linkToOffender.Substring(linkToOffender.IndexOf('=') + 1);
                            if (totalList.Contains(offenderId))
                            {
                                continue;
                            }
                            totalList.Add(offenderId);
//----- ^^^^^ Add the links/ids to a list for later-----
                        }
                        catch (Exception e)
                        {
// ignore this error catch.... its not relevant 
                            if (totalList.Count < 5 && countyId != 21)
                            {
                                if (Program.SiteDownCounter < 4)
                                {
                                    if (Program.LastDown != DateTime.MinValue)
                                    {
                                        if ((DateTime.Now - Program.LastDown).TotalMinutes > 30)
                                        {
                                            Program.sendMail("NY State website seems to be down... will suspend action for 30 minutes. Current time: " + DateTime.Now, "NY State Site Down!");
                                            Program.LastDown = DateTime.Now;
                                            Program.SiteDownCounter++;
                                            for (int x = 0; x < 30; x++)
                                                Thread.Sleep(1000);
                                        }
                                        else
                                        {
                                            Thread.Sleep((1800 - (int)((DateTime.Now - Program.LastDown).TotalSeconds)) * 1000);
                                        }
                                    }
                                    else
                                    {
                                        Program.sendMail("NY State website seems to be down... will suspend action for 30 minutes. Current time: " + DateTime.Now, "NY State Site Down!");
                                        Program.LastDown = DateTime.Now;
                                        Program.SiteDownCounter++;
                                        for (int x = 0; x < 30; x++)
                                            Thread.Sleep(1000);
                                    }
                                }
                                else
                                {
                                    start = false;
                                    break;
                                }
                                continue;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    if (loadedSave.CompletedList != null)
                        totalList = loadedSave.CompletedList;
                    else
                    {
                        Console.WriteLine("The hell?");
                    }

                }
                Program.LastDown = DateTime.MinValue;
                Program.SiteDownCounter = 0;
                ScrapeLogic(countyId, out2, loadedSave, totalList);
            }

一旦收集完 links:

 private void ScrapeLogic(int countyId, string value, ScraperStateSave LoadedSaveState, List<string> total)
        {
            ScraperStateSave saveState = new ScraperStateSave();
            saveState.CountyId = countyId;
            int totalCompletedCount = (LoadedSaveState != null ? LoadedSaveState.CompletedCount : 0);
            int instanceCompletedCount = 0;
            for (int l = totalCompletedCount; l < total.Count; l++)
            {
                try
                {
                    if (Program.SiteDownCounter >= 3)
                        throw new Exception("Shutdown");
                    webManager.driver.Navigate().GoToUrl(getOffenderLinkById(total[l]));
                    string offenderId = total[l];
                    var currentPlacement = webManager.getElementTextByxPath(currentPlacementxPath, true);
                    Boolean wanted = false;
                    try
                    {
                       IWebElement wantedLabel = webManager.driver.FindElement(By.XPath("//*[@id=\"mainContent\"]/h3[2]"));
                       wanted = true;
                    }
                    catch (NoSuchElementException)
                    {

                    }
                    var lastName = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 2));
                    var firstName = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 3));
                    var middleName = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 4));
                    var dob = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 5));
                    var sex = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 6));
                    var riskLevel = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 7));
                    var designation = webManager.getElementTextByxPath(getOffenderInfoBasic(1, 8));
....and more of the same

线程如何启动:

 public NYScaper(Boolean local, Boolean quiet, int id)
    {
        this.localScrape = local;
        this.threadId = id;
        this.quiet = quiet;
        Thread t = new Thread(doScrape);
        t.Start();
    }

一些可能有问题的代码:

  public IWebElement getElementByxPath(string xpath) 
        {
            return driver.FindElement(By.XPath(xpath));
        }


        public string getElementTextByxPath(string xpath)
        {
            return driver.FindElement(By.XPath(xpath)).Text;
        }

        public string getElementTextByxPath(string xpath, Boolean wait)
        {
            return new WebDriverWait(driver, TimeSpan.FromSeconds(2)).Until(ExpectedConditions.ElementExists((By.XPath(xpath)))).Text;
        }

主要problem/Overview:

适用于 Windows 7 Professional 但不适用于 2003 服务器!错误似乎只出现在 link 收集和数据收集之间或数据收集开始时。有两个线程,它们可以在本地 windows 7 桌面上正常工作。如果程序重新启动,它将加载 links 的保存,并且它将与保存的 links 一起工作,而不会显示错误!

当浏览器未显示为顶部屏幕时会出现此错误。所以如果你最小化它,这最终会发生。

目前尚无解决方案,请参阅 https://code.google.com/p/selenium/ 了解有关此问题的更新。