2015-09-24 25 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

相关问题