[英]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.