2015-04-07 76 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"); 
相关问题