让我先告诉你这个错误,然后我将解释上下文,最后我将显示代码并解释它。
错误
以下是使用FirefoxDriver时的相同/类似错误
上下文
我制作了一个程序来浏览网站并收集一些数据。这
程序在我的本地桌面windows7专业版上100%工作
但是当我将它移动到我的服务器上时,它是一个带有.net框架3.5的windows 2003服务器,它抛出了上述错误。
注意,在上面的情况下,应用程序是多线程的,有两个线程运行2个selenium实例。当他们收集完要浏览的链接列表时,应用程序就会出现问题。一个线程将逐一查找并遍历链接列表,当另一个线程完成收集要探索的链接时,两个selenium客户端都会中断并开始抛出上面的错误。
我没有使用任何不适用于.net框架3.5的功能……所有的功能都是标准化的,以适合2003服务器(至少据我所知)。
密码
当它正在收集链接时:
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);
}
一旦完成收集链接:
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;
}
主要问题/概述:
适用于Windows 7 Professional,但不适用于2003服务器!错误似乎只出现在链接收集和数据收集之间或数据收集开始时。有两个线程,它们在本地windows7桌面上工作没有问题。如果程序重新启动,它将加载保存的链接,并将使用这些保存的链接而不会显示错误!