2015-10-02 44 views
2

我得到一個錯誤如何簡化href屬性,所以我沒有得到「給定的路徑格式不被支持」?

特定的路徑格式不支持

但是當我使用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); 
} 
+0

將[url轉換爲有效的文件名並返回url ](http://stackoverflow.com/questions/4423200/translate-url-to-a-valid-file-name-and-back-to-url) – JeffC

回答

1

如果目標是獲得的,除了特定的鏈接頁面上的鏈接列表,也許這將更好地工作

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, '_'); 
    } 
    ... 
} 
+0

目標是使鏈接[我]可行的使用申請一個名字。像http //:example.com.jpg,但我希望它像httpexamplecom.jpg – safary

相關問題