Arjun Dev
Arjun Dev

Reputation: 414

Selenium upload with sendkeys not working- Since there is a popup before uploading

I have a scenario where the upload of a file in the webpage which have a browse button and clicking this browse button, the the windows explore window appears. I select the file and click the upload button in the window. Now, there is a overlay popup on the webpage to enter the name of uploading file and click the save button.

This cannot be automated since there is a popup overlay screen before getting uploaded. The below is the code that I have used.

WebElement PDFUpload =driver.findElement(By.xpath("(//div[@class='upload-area'])[2]"));
PDFUpload.sendKeys("C:\\test\\Testuploads\\test.pdf");

This working fine when there is no overlay screen after clicking the upload after selecting the file. But when the overlay for naming the uploaded file is implemented in webpage, the test is not running.Tried with robot class as well, its still not working(might be because of the robot class which I put incorrectly). But leave this case because I dont see using robot class is a good practice on this kind of dynamic website which I am working on. Can anyone help?

Upvotes: 0

Views: 635

Answers (2)

Sachin Ramdhan Boob
Sachin Ramdhan Boob

Reputation: 345

You can try removing the overlay screen through the automation code itself and then upload file.

Set the css display property to none using Javascript.

Execute javascript through webdriver

argument[0].style.display = "none";

Where argument[0] is the WebElement referencing to the overlay screen.

I understand this approach is quite debatable as this modifies the original web page, however, if the more important functionality to test is the file upload then it should be okay.

Upvotes: 0

Krishna Barri
Krishna Barri

Reputation: 1073

This method is for handling the Windows File Upload dialogue, which cannot be handled using Selenium. Please follow the below steps:

  • Click on the File Upload / Choose File button, so that the File Upload dialog is displayed.
driver.findElement(By.id("uploadbutton")).click;
  • Copy your file's absolute path to the clipboard

StringSelection ss = new StringSelection("D:/Test/Test1.docx"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

  • Paste the file's absolute path into the File name field of the File Upload dialog box
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();

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);

Upvotes: 1

Related Questions