2015-05-15 34 views
1

我無法上傳文件,AutoIt的V3硒的webdriver - 無法上傳文件,AutoIt的

嘗試用很多方法,但沒有運氣得到它的工作

方法之一:(get Element is not currently visible

driver.findElement(By.xpath("//input[@type='file']")).click(); 
Runtime.getRuntime().exec("D:\\Documentation\\Script To Upload File.exe"); 

方法二:(get Element is not currently visible

driver.findElement(By.name("fileName")).click(); 
Runtime.getRuntime().exec("D:\\Documentation\\Script To Upload File.exe"); 

方法三:( PASSED: testCaseOne, but no file is uploaded

driver.findElement(By.id("button2")).click(); 
Runtime.getRuntime().exec("D:\\Documentation\\Script To Upload File.exe"); 

對於附加鏈接,當我與firepath檢查,這將涉及到的XPath "html/body/input" 1個匹配的節點

這裏我的html文件

<input type="file" name="fileName" style="position: absolute; margin: -5px 0px 0px -175px; padding: 0px; width: 220px; height: 30px; font-size: 14px; opacity: 0; cursor: pointer; display: none; z-index: 2147483583; top: 457px; left: 459px;"/> 

這裏的HTML代碼文件上傳部分:

<html> 
    <body> 
     <div> 
      <div> 
       <form> 
        <div> 
         <dl> 
          <dd class="attachFile"> 
           <div class="attachUpload"> 
            <a id="button2" class=" "> 
             <img class="attachIco" alt="" src="http://qa.seleniumqa.com/ga/en/clean/images/BLANK.GIF"/> 
              Attach file 
            </a> 
           </div> 
          </dd> 
         </dl> 
        </div> 
       </form> 
      </div> 
     </div> 
    </body> 
</html> 

腳本要上傳File.au3代碼

; It will wait for 8 seconds to appear File Upload dialog. 
; Used Title property of File upload dialog window. 

    WinWait("File Upload","",8) 

; Set control focus to File name Input box of File Upload dialog. 
; Used Class property of File upload dialog window and Class+Instance property for File name Input box. 

    ControlFocus("[CLASS:#32770]","","Edit1") 

    Sleep(3000) 

; Set the name of file In File name Edit1 field. 
; "Test.txt" file Is located In AutoIT folder of E drive. So we have to provide full path like E:\AutoIT\Test.txt. 

    ControlSetText("[CLASS:#32770]", "", "Edit1", "D:\Documentation\uploadFile.xls") 

    Sleep(3000) 

; Click on the Open button of File Upload dialog. 

    ControlClick("[CLASS:#32770]", "","Button1"); 

編輯:分享與那些在未來

需要這個代碼不工作的解決方案(不知道爲什麼)

driver.findElement(By.id("button2")).sendKeys("D:\\Documentation\\uploadFile.xls"); 

但在這樣,它的工作

driver.findElement(By.id("button2")).click(); 
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("D:\\Documentation\\uploadFile.xls"); 
+0

問題是什麼?你不知道如何檢查元素是否被加載? –

+0

Hi @Ya Wang,文件未上傳 – christo

+0

爲什麼不使用'Robot'代替?看到這個線程 - http://sqa.stackexchange.com/questions/12851/how-can-i-work-with-file-uploads-during-a-webdriver-test/12872#12872 – LittlePanda

回答

1

輸入控件的類型是文件,因此使用下面的代碼行來上傳文件應該可行。

WebElement e = driver.findElement(By.id("the id")); 
e.sendKeys("file path"); 
+0

感謝您的幫助,不知道爲什麼'driver.findElement(By.id(「button2」))。sendKeys(「D:\\ Documentation \\ uploadFile.xls」);'不起作用,'driver.findElement 。By.id( 「按鈕2」))點擊(); ''driver.findElement(By.xpath(「// input [@ type ='file']」))。sendKeys(「D:\\ Documentatio n \\ uploadFile.xls」);''是工作。 Nvm,只要它工作,對我來說很好 – christo

0

這是因爲在你的Html文件中輸入type = file。如果您的輸入類型是其他東西,您需要嘗試使用AutoIt或RobotApi。