2016-03-04 74 views
0

我想附上一個文件上傳。測試的一部分,我在嘗試以下,但它不工作Selenium WebDriver c#是否可以附加文件上傳

1) Enter some details into the input fields  
2) Click Browse on the Web site 
3) Enter a file path in the windows popup that appears 
4) Click Open on the windows popup 
5) Click Apply on the Web site 

Driver.FindElement(By.Id("Name")).SendKeys(name); 
Driver.FindElement(By.Id("EmailAddress")).SendKeys(email); 
Driver.FindElement(By.Id("TelephoneNumber")).SendKeys(telephone.ToString());  
Driver.FindElement(By.Name("file")).Click();  
Driver.SwitchTo().ActiveElement().SendKeys("/home/likewise-open/GLOBAL/123/Documents/filename.txt"); 
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); 
Driver.FindElement(By.Id("convertButton")); 
+2

http://stackoverflow.com/questions/11256732/how-to-handle-windows-file-upload-using-selenium-webdriver 可能重複 –

回答

1

由於webdriver的只能處理基於Web的交互,而不是基於Windows的, 則需要以上傳或下載並整合AutoIt的效用它在Selenium腳本中。 AutoIT是一款免費工具。

+0

是的,請參閱我的答案在這裏:https://stackoverflow.com/questions/14592853 /如何上傳文件在selenium沒有文本框#48578616 – SharpC

1

只是試試這個代碼

Driver.FindElement(By.Name("yourUploadFileTextBox")).SendKeys("/home/likewise-open/GLOBAL/123/Documents/filename.txt"); 
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); 
Driver.FindElement(By.Id("convertButton")); 

它將工作。無需點擊瀏覽按鈕。只需在顯示文件路徑的文本框上發送鍵。

+0

它現在工作,謝謝 – Dulku

+1

請標記爲答案,如果有幫助。 – joinsaad

相關問題