简体   繁体   中英

How to close windows file upload window when using selenium

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>

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

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


}

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