如何简化href属性,使我不得到“给定路径格式不支持”
本文关键字:路径 格式 不支持 属性 href 何简化 | 更新日期: 2023-09-27 18:15:53
报错
指定的路径格式不支持
但是当我使用driver.Title
而不是links[i]
时,它可以正常工作,只是有这么多相同的标题,因为对我来说最好使用href,但我猜你不能在文件名中使用":"或"/",所以如何简化href,所以我不会得到"不支持的路径"错误?
int linkCount = driver.FindElements(By.CssSelector("a[href]")).Count;
string[] links = new string[linkCount];
List<IWebElement> linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
for (int i = 0; i < linkCount; i++)
{
links[i] = linksToClick[i].GetAttribute("href");
}
for (int i = 0; i < linkCount; i++)
{
driver.Navigate().GoToUrl(links[i]);
ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
Screenshot screenCapture = screenshotDriver.GetScreenshot();
screenCapture.SaveAsFile(Path.Combine(testPath, links[i] +"_"+ testScreenshotTitle),
System.Drawing.Imaging.ImageFormat.Png);
}
如果目标是获得除了特定链接之外的页面上的链接列表,也许这会更好
using System.Link;
var blackList = {"LogOff", ...};
var links = driver
.FindElements(By.CssSelector("a[href]"))
.Select(a => a.GetAttribute("href"))
.Where(u => !blackList.Any(s => s.Contains(u)));
foreach (string link in links)
{
...
}
净化文件名
foreach (string link in links)
{
var fileName = Path.Combine(testPath, link + "_" + testScreenshotTitle;
foreach (char c in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '_');
}
...
}