2015-09-24 24 views
0

我正在使用webdriver和nunit在selenium中編寫腳本來自動化我的web應用程序。我必須在我的應用程序中上傳文件。但無法做到。如何在Windows 7中處理selenium中的文件上傳

單擊瀏覽按鈕時打開對話框,並在選擇文件時上載文件。

瀏覽

我該如何在硒中做到這一點?

+0

可能重複://計算器/我的問題/ 16896685/how-to-upload-files-using-selenium-webdriver-in-java) –

回答

0
Answer is already available on stack, still I am repeating: 

發現上傳按鈕,創建這個webelement,並按照此代碼:

//open upload window 
    upload.click(); 

    //put path to your image in a clipboard 
    StringSelection ss = new StringSelection(<give file location>); 
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 

    //imitate mouse events like ENTER, CTRL+C, CTRL+V 
    Robot robot = new Robot(); 
    robot.keyPress(KeyEvent.VK_ENTER); 
    robot.keyRelease(KeyEvent.VK_ENTER); 
    robot.keyPress(KeyEvent.VK_CONTROL); 
    robot.keyPress(KeyEvent.VK_V); 
    robot.keyRelease(KeyEvent.VK_V); 
    robot.keyRelease(KeyEvent.VK_CONTROL); 
    robot.keyPress(KeyEvent.VK_ENTER); 
    robot.keyRelease(KeyEvent.VK_ENTER); 
+0

我正在使用C#與硒。我試過這個driver.FindElement(By.Id(「pickfiles」))。SendKeys(「D:\\ file1.rar」); 但它不工作。 我也嘗試使用AutoIT,但無法運行由AutoIT提供的exe文件 –

+0

我的文件中的元素是 它使用plupload javascript API來上傳文件 –

+0

我解決了這個問題引用此鏈接 http://stackoverflow.com/questions/15531728/tdd-for-plupload -with-django-splinter –

0
public void uploadFile(String locatorId, String filePath) { 

WebElement fileInput = findElementExplicitWaitWithNoVisbility(By.id(locatorId), true); 

fileInput.clear(); 
fileInput.sendKeys(filePath);} 
[如何使用Java中硒的webdriver上傳文件(HTTP的
+0

請解釋你的代碼。 – Maroun

相關問題