2015-04-07 112 views
-1

嗨,你可以幫助我在Safari,Mac OS中上傳文件。如何使用selenium webdriver在SafariDriver/Safari中上傳文件?

我已經給了一個位點,我們可以通過點擊 「選擇文件」 上傳以下類型文件的按鈕(JPG,PNG,GIF,DOC,XLS,PDF,ZIP,RAR,ZIPX)

package Default; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.safari.SafariDriver; 

public class Safari_demo { 

    public static void main(String[] args) throws InterruptedException{ 

     WebDriver driver = new SafariDriver(); 

     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

     Thread.sleep(6000); 

     driver.get("http://www.files.com/"); 

     driver.findElement(By.id("uploadFields")).click(); 

     //can you please help me with code in this line 
     //this is the place were i need to enter the address of the 
     //file which is in my system 

     driver.close(); 
    } 

} 
+0

driver.findElement(By.id( 「youruploadbuttonid」))的SendKeys( 「yourpicturelocalpath」); –

回答

0

所以文件上傳對話框實際上是一個本地系統對話框而不是網頁對話框,因此您無法使用Selenium與它進行交互。要做到這一點本地的方法是將路徑發送到文件路徑文本框,saucelabs把它描述得很好:

對於那些你在本地這樣做,所有你需要做的就是使用 SendKeys命令鍵入本地任何文件字段中的文件路徑。 這在所有司機中都很有魅力。當將此測試移動到 遠程服務器(例如我們的Selenium 2 Cloud)時,您需要做的全部工作就是使用setFileDetector方法讓WebDriver知道您正在從本地計算機上傳文件到 遠程 服務器,而不是隻輸入一個路徑。幾乎神奇的是,在編寫固定遠程路徑之前,文件將會以base64編碼並透明地通過JSON線 Protocol發送給您。這是一個很好的解決方案,因爲它可以讓您將測試從本地切換到 遠程驅動程序,而無需擔心更改測試代碼。

https://saucelabs.com/resources/selenium-file-upload

1

嘗試。

fd.findElement(By.xpath(".//*[@id='uploadFields']/input")) 
       .sendKeys(
         "your image path"); 
相關問題