简体   繁体   中英

Upload a file using selenium webdriver WITHOUT using Robot API or selenium command send keys

I have a web application which is using Outsystem for UI. So the File upload button is of Type = 'submit' and not Type='file' hence the selenium code

    driver.findElement(By.id("browse")).sendKeys("/path/to/the/file");

used to upload a file is not working for me. I have tried the code for Robot API as well

WebElement ele = driver.findElement(By.id("Browse"));
ele.click();    
StringSelection ss = new StringSelection(FilePath); 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);                                     
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);    

Which is sometimes working fine , but failing most of the time because the focus is removed from the window. My Script will run for a long time, in such scenario keeping the focus always on the window is not a feasible idea. Can someone suggest me any other way to tackle the upload file process in Selenium?

我在这里了解到的是您无法等到弹出窗口弹出时出现,因为我们无法明确等待窗口弹出,因此在启动机器人之前,我通过放置Thread.sleep(2000)解决了此问题类,以便可以在我们开始使用它之前显示弹出窗口。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM