简体   繁体   中英

Can not close upload file window in selenium

I have a question that I try to resolve. I have a screen in my webpage, that open a dialog box to upload a file. I managed to select the file, However the dialog box not closed, and stayed open. So I can not proceed with the other things on screen. I want to close the dialog box. I try to close the driver and it is closed the web page. I try to create a function of all the windows opened and it is not find the dialog box. How can I close this dialog box? this is what I done that not worked since it is not recognized the dialog box. (says only one window is opened) The problem is that in the devtool I do not find the locator that closed this dialog box, since I can not inspect it, can someone advise just how to close this? since the browser is stuck,

 Thread.sleep(8000);
        Set <String> w = deiver2.getWindowHandles(); // create set of all windows
        deiver2.switchTo()
                .activeElement();
        System.out.println("Window title: "+ deiver2.getTitle());

        deiver2.findElement(By.xpath("//input[@type='file']"))
        .sendKeys(
                "X:\\AutomationFiles\\yoyoy.pdf");
        for (String h: w){
            deiver2.switchTo().window(h);
            String s= deiver2.getTitle();
            System.out.println("Window title: "+ deiver2.getTitle());
            if(s.equalsIgnoreCase("Open")){
                System.out.println("Window title to be closed: "+ deiver2.getTitle());
                deiver2.close();
            }
        }
        deiver2.switchTo().window(base);
        System.out.println("END");

在此处输入图像描述

在此处输入图像描述

This most likely is a separate browser window and has it's own dev tools. To validate that, you can focus it and press F12 for the dev console . I see that you already getting the window handle. Put a debugger and try to do that thing with the dev tools. You will get the selector with which you can play around in the dev tools while you are under breakpoint

the solution I found is just not to open the select file dialog. I saw that if I just sent keys to the input without clicking the upload file button, that open the dialog box is working

 public  void uploadFile() throws Exception {
    WebDriver deiver2 = getWebDriver();
 
    Thread.sleep(3000);
    deiver2.switchTo()
            .activeElement();
    deiver2.findElement(By.xpath("//input[@type='file']"))
    .sendKeys(
            "X:\\AutomationFiles\\yyyyyy.pdf");

    System.out.println("END");

}

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