33

我是一个javascript/java开发人员,我一直在试图弄清楚selenium webdriver自动化框架如何从文件系统上传文件。通过JavaScript设置文件输入是不可能的,因为这是违反安全的。但不知怎么的webdriver可以用下面的命令来做到这一点:selenium webdriver如何将文件上传到浏览器?

driver.setFileDetector(new LocalFileDetector()); 
WebElement upload = driver.findElement(By.id("myfile")); 
upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg"); 
driver.findElement(By.id("submit")).click(); 

因此,他们正在设置发送键,值吗?我不明白。我已经查看了通过在这里找到的源代码: http://code.google.com/p/selenium/source/checkout 我仍然无法找到他们在哪里做到这一点。

编辑:我的问题不是如何用硒做到这一点,但硒开发商是如何做到这一点的?他们是如何解决javascript中的安全限制的?他们如何上传文件?

+0

我的第一个猜测是,该按钮元素有一个“价值”的微博,增加的SendKeys字符的价值? – djangofan

回答

17

尼斯问题哥们......他们已经写了一个HTTP代理服务器来解决的Javascript secuirty限制。通过使用此代理,可以避免“相同主机源”策略的许多限制,浏览器不允许Javascript调用除当前页面所服务的服务器以外的其他任何内容。

此外WebDriver使用触发OS级别事件的替代方法。由于这些“本地事件”不是由浏览器生成的,因此这种方法避开了对合成事件的安全限制,并且因为它们是操作系统特定的,所以一旦它们在特定平台上为一个浏览器工作,则在另一个浏览器中重用代码是相对简单。

大部分内容上面从引用below..do阅读硒更多详细信息如下参考的内部构件

http://www.aosabook.org/en/selenium.html

+0

请参阅下面的答案。 – djangofan

0
//assuming driver is a healthy WebDriver instance 
    WebElement fileInput = driver.findElement(By.name("uploadfile")); 
    fileInput.sendKeys("C:/path/to/file.jpg"); 

driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg"); 

试试这个,让我知道

+0

对不起,我不清楚我的问题。我增加了对这个问题的澄清。我想知道硒的开发者是如何完成你所描述的可能性的。 – justspamjustin

0

在某些情况下,特别是与Java中,你需要创建一个文件对象,通过absolutePath()到驱动程序,如下所示:

File file = new File(sampleFile); 
driver.findElement(By.id("<Your input tag with type of File>")).sendKeys(file.getAbsolutePath()); 

样品文件是指向需要被上传的文件的字符串。 这适用于Firefox和Chrome浏览器。

4

上传WINDOWNS文件功能的HTML代码为:

<input id="fileField" type="file" onchange="document.getElementById('textfield').value=this.value" name="position"> 

<input type="submit" value="导入"> 

您可以使用下面的代码来完成上传Windows文件。它工作成功,代码不包括点击上传操作。

driver.FileDetector = new LocalFileDetector(); 
FindElement(By.Id("fileField")).SendKeys(@"C:\Users\admin\Desktop\ProfessionCodes.txt"); FindElement(By.CssSelector("input[type='submit']")).Click(); 
2

我在Facebook 上传照片使用硒的webdriver和AutoIt的

步骤如下

步骤1

在蚀代码高达(上传照片)是如下:

WebElement Upload = Firefox.findElement(By.cssSelector("input[id^='u_']")); 
Upload.click(); 

步骤2

下载和安装的AutoIt:http://www.autoitscript.com/site/autoit/downloads/(下载ZIP)

步骤3

编写代码,如下在记事本中,并保存它作为PhotoUpload.au3

WinWaitActive("File Upload") 
Send("D:\Photo0116.jpg") 
Send("{ENTER}") 

步骤4 :右键单击这个.au3文件&编译它。

步骤5:在下面的脚本文件添加代码:

try { 
    String[] commands = new String[]{}; 
    // Location of the autoit executable 
    commands = new String[] {"D:\\My softwares\\install software\\selenium\\UploadPhoto3.exe"}; 
    Runtime.getRuntime().exec(commands); 
}  
catch (IOException e) {} 

步骤6:运行脚本(PhotoUpload.java

步骤7:照片获得成功上传。

+0

你可以说一些截图编码和用户界面? –

0

帮我做文件上传,

代码:

public class FileUpload { 
     @Test 
     public void test() { 
      WebDriver driver = new FirefoxDriver(); 
      driver.get("http://www.freepdfconvert.com/pdf-word"); 
      driver.findElement(By.id("clientUpload")).click(); 
      driver.switchTo() 
        .activeElement() 
        .sendKeys(
          "/home/likewise-open/GLOBAL/123/Documents/filename.txt"); 
      driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
      driver.findElement(By.id("convertButton")); 
      /* 
      * driver.switchTo().activeElement() 
      * .sendKeys("selenium_2_testing_tools.pdf"); ; 
      */ 
     { 
       driver.wait(30000); 
      } catch (Exception er) { 
       System.out.println(er); 
      } 

     } 
    } 
相关问题