2

I am trying to write selenium tests for a website using java. However, I have come across a problem when testing file uploading.

When I click the file upload button, it automatically opens the windows file upload. I have code working to put the file path ("D:\\test.txt") into selection. From researching this subject I understand there is no way for selenium webdriver to handle this. So my question is this: what is a way I can simply close the upload window in an automated way? Indeed the sendKeys working with selecting the txt file but the window upload still not closing.

Thanks in advance

ProductActionCode :

public static void AutoInsert_Execute(WebDriver driver) throws Exception {

        ConfirmationPlaceBet_Page.btn_ChanShiOdd(driver).click();

        ConfirmationPlaceBet_Page.btn_UploadOddTxt(driver).click();
        ConfirmationPlaceBet_Page.btn_DocumentToBeUpload(driver).click(); 
        ConfirmationPlaceBet_Page.pick_DocumentToBeUpload(driver).sendKeys("D:\\test.txt");
        ConfirmationPlaceBet_Page.btn_ProceedUploadAuto(driver).click();
        ConfirmationPlaceBet_Page.btn_ConfirmedUploadAuto(driver).click();

        for (int i = 0; i < 3; i++) {
            ConfirmationPlaceBet_Page.btn_AddDoubleBet(driver).click();
        }

        ConfirmationPlaceBet_Page.btn_ConfirmNumberToBet(driver).click();

        for (int k = 0; k < 49; k++) {
            ConfirmationPlaceBet_Page.btn_IncreaseBet(driver).click();
        }

        ConfirmationPlaceBet_Page.btn_ProceedBet(driver).click();

        ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();
    }

ConfirmationPlaceBetCode :

public static WebElement pick_DocumentToBeUpload(WebDriver driver) throws Exception{
        try{ 
             driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
             element = driver.findElement(By.name("file"));
             Thread.sleep(500);

             //Log.info("Pick Lottery1 ");              
        }catch (Exception e){
            Log.error("Button is not found on the Confirmation Page");
            throw(e);
            }
        return element;
    }

HTML CODE :

<div id="filePicker" class="webuploader-container"><div class="webuploader-pick">选择文件</div><div id="rt_rt_1a24olu914nt122e1qls1c5l1b2qm" style="position: absolute; top: 0px; left: 0px; width: 86px; height: 30px; overflow: hidden; bottom: auto; right: auto;"><input type="file" name="file" class="webuploader-element-invisible" multiple="multiple" accept="text/*"><label style="opacity: 0; width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);"></label></div></div>
8
  • does it not working ? ConfirmationPlaceBet_Page.pick_DocumentToBeUpload(driver).sendKeys("D:\\test.txt"); it should close close window automatically. Commented Oct 21, 2015 at 7:55
  • @HelpingHands Hi , it's not closing anywhere and the testCases fail directly Commented Oct 21, 2015 at 7:57
  • I need a clarity on Question now. You are able to upload the File ? From where this new window comes ? If upload file is on new window then store this window and switch to new window, complete the task there and close it and switch back to original window. Commented Oct 21, 2015 at 8:04
  • i can't upload anything , when i manual close the upload window , indeed the file is picked as listed on the field in textbox. Commented Oct 21, 2015 at 8:08
  • Still the Question is not clear to me!!! Commented Oct 21, 2015 at 8:19

2 Answers 2

2

You need to use sendkeys for same.

I assuming that you have a browse button and a button as upload

driver.findElement(By.xpath("YOUR XPATH")).sendKeys("Absolute path of file");

Feel free to change locator of an element in the above code

sendkeys will set the path and file name in the HTML for the respective upload field

Now click on upload button.

ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();

Note:- put a wait between sendkeys and click on upload button. It helps many times

For more info refer below link:-

http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver

Hope it will help you :)

Sign up to request clarification or add additional context in comments.

Comments

2

You can use these lines of code to close the upload window after sending the path of file with pressing ESCAPE Button

import java.awt.Robot;
import java.awt.event.KeyEvent;

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);

For Example look at below code:

    public static void main(String[] args) throws InterruptedException, AWTException {
    System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");

    WebDriver driver = new FirefoxDriver();
    driver.get("https://online2pdf.com/reduce-pdf-file-size");

    WebElement element =driver.findElement(By.xpath("//*[contains(text(),'Select files')]"));
    Actions ac = new Actions(driver);
    ac.moveToElement(element).click().build().perform();
    driver.switchTo().activeElement().sendKeys("C:\\Users\\eclipse-workspace\\Selenium\\abc.pdf");
    Thread.sleep(300);

     Robot robot = new Robot();
     robot.keyPress(KeyEvent.VK_ESCAPE);
     robot.keyRelease(KeyEvent.VK_ESCAPE);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.