首先让我告诉你错误然后我将解释上下文,最后我将展示代码并解释。
上下文
我制作了一个程序来浏览网站并收集一些数据。这个程序在我的本地桌面Windows 7专业版上100%工作,但当我将它移动到我的服务器,这是一个带有.net framework 3.5的Windows 2003服务器时,它会引发上述错误。
请注意,在上面的情况下应用程序是多线程的,有两个线程运行2个selenium实例。当应用程序完成收集他们想要探索的链接列表时,就会出现问题。一个线程将工作查找并逐个浏览链接列表,当其他人完成收集链接时,它想要探索两个selenium客户端中断并开始抛出上述错误。
我没有使用任何不适用于.net framework 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服务器!链接收集和数据收集之间或数据收集开始时似乎只出现错误。有两个线程,它们在本地Windows 7桌面上工作没有问题。如果程序重新启动,它将加载链接的保存,它将与这些保存的链接一起使用,而不显示错误!
答案 0 :(得分:0)
当浏览器未显示为顶部屏幕时,会发生此错误。因此,如果你最小化它,这将最终发生。
目前还没有解决方案,因为有关此问题的更新,请参阅https://code.google.com/p/selenium/。