gpt4 book ai didi

c# - 如何简化 href 属性,这样我就不会得到 "the given paths format is not supported"?

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:14 24 4
gpt4 key购买 nike

我得到一个错误

the given paths format is not supported

但是当我使用 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, '_');
}
...
}

关于c# - 如何简化 href 属性,这样我就不会得到 "the given paths format is not supported"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com