简体   繁体   English

无法关闭 selenium 中的上传文件 window

[英]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.我设法 select 文件,但是对话框没有关闭,并保持打开状态。 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.我尝试关闭驱动程序,它关闭了 web 页面。 I try to create a function of all the windows opened and it is not find the dialog box.我尝试在所有打开的 windows 中创建一个 function,但找不到对话框。 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? (说只有一个 window 被打开)问题是在开发工具中我没有找到关闭此对话框的定位器,因为我无法检查它,有人可以建议如何关闭它吗? 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.这很可能是一个单独的浏览器 window 并拥有自己的开发工具。 To validate that, you can focus it and press F12 for the dev console .要验证这一点,您可以将其聚焦并按F12 for the dev console I see that you already getting the window handle.我看到您已经获得了 window 手柄。 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.我找到的解决方案就是不要打开 select 文件对话框。 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");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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