Selenium ChromeDriver将始终无法加载Web资源,例如Bootstrap,JQuery等



这一切都在标题中。我将使用下面的代码启动chrome驱动程序,它只会加载JQuery,Bootstrap等资源。发生这种情况时,加载页面也需要特别长的时间。我会收到错误,指出资源由于超时而无法加载。

我对我需要做什么一无所知,所以任何有人愿意抛出我的方式帮助他们找到解决方案的文档,或者类似的东西,将不胜感激。这是我尝试过的: 1( 使用ChromeOptions对象禁用代理设置 2( 禁用 SSL 证书 3( 禁用平均 4( 禁用防火墙 5(我的房间互联网连接很好

我尝试做的另一件事是查看带有ChromeOptions.SetLogginPreference(string, logLevel)的日志文件,该文件在ChromeDriver75中有效,但在ChromeDriver76中不再有效。用于查看日志的相同代码在驱动程序中的工作方式不同。

//SETTING UP THE DRIVER WITH SOME OPTIONS
public static IWebDriver GetDriver()
{
IWebDriver iwd;
ChromeOptions co = new ChromeOptions(){Proxy = null};
co.AddAdditionalCapability("ACCEPT_SSL_CERTS", true, true);
co.AddAdditionalCapability("ACCEPT_INSECURE_CERTS", true, true);
co.AddArguments("--ignore-certificate-errors", "--ignore-ssl-errors");
co.SetLoggingPreference("all", logLevel: LogLevel.All);
iwd = new ChromeDriver("path/to/driver", co);
}
//ME TRYING TO LOAD THE SITE 10 TIMES
public bool TryLoad()
{   
//bool for if it was completed or not
bool b = false;

for (int i = 0; i < 9; i++)
try
{
Console.WriteLine();
Gen.Write("rnLoading New Page==>", false, ConsoleColor.Cyan);
p.iwd_Driver.GoTo(p.sURL);
if (WasEverythingWasLoaded(p))
{
Gen.Write(" (/) Page Done!", true, ConsoleColor.Green);
b = true;
return b;
}
Gen.Write($"{(i == 0 ? "Failed to load resources" : $"Failed to load resources {i + 1} times")}.", false, ConsoleColor.Red);
}
catch (WebDriverException e)
{
Gen.Write($"rnrnFATAL::Failed to load the page {p.sURL}rnException info: {e}rnrn", true, ConsoleColor.Red, ConsoleColor.White);
}
finally
{
if (!b)
Gen.Write($"rnAttempting again==>", true, ConsoleColor.DarkYellow);
}
try
{
Gen.Write("rnLoading New Page==>", false, ConsoleColor.Cyan);
p.iwd_Driver.GoTo(p.sURL);
if (WasEverythingWasLoaded(p))
{
Gen.Write(" (/) Page Done!", true, ConsoleColor.Green);
b = true;
return b;
}
}
catch (WebDriverException e)
{
Gen.Write($"Something is very wrong and needs to be acknowledged here... try flushing the DNS, restarting VS, restarting computer," +
$" canceling some services etc. because it isn't just Selenium that is being a problem here...rnException Info: {e}");
}
catch (Exception e)
{
Gen.Write($"rnrnFATAL::Exception when loading {p.sURL}rnException info: {e}rnrn", true, ConsoleColor.Red, ConsoleColor.DarkGray);
}
finally
{
if (!b)
p.iwd_Driver.Terminate();
}
return b;
}
//trying to interogate the log files. I want to check to see if everything was 
//successfully loaded. If not, then I'm going to try again. This works with
// chormedriver75 but not with chromedriver76 for some reason...
private static bool WasEverythingWasLoaded(Page p)
{
ILogs v = p.iwd_Driver.Manage().Logs;
IReadOnlyCollection<LogEntry> logs;
try
{
logs = p.iwd_Driver.Manage().Logs.GetLog(LogType.Browser);
}
catch (NullReferenceException)
{
Gen.Write("Looks like I can't take logs with this version...", true, ConsoleColor.Blue, ConsoleColor.White);
return true;
}
//spit out the logs
foreach (LogEntry le in logs)
Gen.Write($"LogEntry: {le.Message}", true, ConsoleColor.Red, ConsoleColor.White);
//if the count of the relevant logs is greater than a certain amount, then fail it
return logs.Where(x =>
x.Level == LogLevel.Severe
&& x.Message.ToLower().Contains("failed to load resource")
&& !x.Message.Contains(@"https://wes.sandbook.ca/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)")
).ToList().Count() < 1;
}

期待: 我希望我的驱动程序只需打开并始终转到我要求它的页面。我只了解这些chrome选项在做什么,所以如果它看起来不合适,请告诉我。

找到了修复程序。超级妩媚,但它有效。

这肯定与某些东西变慢并且超时有关。我所做的是获取我需要的所有资源,创建了一个 HTML 文档,该文档只包含

<html>
<head>
<script src="...the_guds..."></script> <!--could be for bootstrap, favicon, or whatever-->
</head>
</html>

对于我需要包含的每个资源,并在初始化驱动程序后系统地加载每个资源。之后,资源被缓存,之后加载速度超快。

相关内容

最新更新