簡體   English   中英

使用硒時如何關閉Windows文件上傳窗口

[英]How to close windows file upload window when using selenium

我正在嘗試使用Java為網站編寫硒測試。 但是,在測試文件上傳時遇到了一個問題。

當我單擊文件上傳按鈕時,它將自動打開Windows文件上傳。 我有一些代碼可以選擇文件路徑("D:\\\\test.txt") 通過研究這個主題,我了解到硒webdriver無法解決這個問題。 所以我的問題是:我可以簡單地自動關閉上傳窗口的方法是什么? 實際上,sendKeys可以選擇txt文件,但窗口上傳仍未關閉。

提前致謝

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代碼:

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

您需要使用sendkey。

我假設您有一個瀏覽按鈕和一個上傳按鈕

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

隨意更改以上代碼中元素的定位符

sendkeys將在HTML中為相應的上載字段設置路徑和文件名

現在點擊上傳按鈕。

ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();

注意:-在sendkey之間等待,然后單擊上載按鈕。 幫助很多次

有關更多信息,請參見以下鏈接:-

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

希望它能對您有所幫助:)

您可以使用以下代碼行在按ESCAPE按鈕發送文件路徑后關閉上傳窗口

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

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

例如看下面的代碼:

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


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM