如何在使用selenium

时间:2015-10-21 07:54:03

标签: java selenium automation

我正在尝试使用java为网站编写selenium测试。但是,我在测试文件上传时遇到了一个问题。

当我点击文件上传按钮时,它会自动打开Windows文件上传。我有代码工作将文件路径("D:\\test.txt")放入选择。通过研究这个主题,我知道selenium 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 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>

2 个答案:

答案 0 :(得分:1)

您需要使用sendkeys。

我假设您有一个浏览按钮和一个按钮作为上传

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

随意更改上述代码中元素的定位器

sendkeys将在HTML中为相应的上传字段设置路径和文件名

现在点击上传按钮。

ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();

注意: - 在sendkeys之间等待并点击上传按钮。它有很多次帮助

有关详细信息,请参阅以下链接: -

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

希望它会对你有所帮助:)。

答案 1 :(得分:0)

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


}