使用 selenium 时如何关闭 windows 文件上传 window
How to close windows file upload window when using selenium
我正在尝试使用 java 为网站编写 selenium 测试。但是,我在测试文件上传时遇到了问题。
当我点击文件上传按钮时,它会自动打开windows文件上传。我有代码可以将文件路径 ("D:\test.txt")
放入选择中。通过研究这个主题,我了解到 selenium webdriver 无法处理这个问题。所以我的问题是:有什么方法可以自动关闭上传 window?事实上,sendKeys 正在选择 txt 文件,但 window 上传仍然没有关闭。
提前致谢
产品操作代码:
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();
}
确认地点投注代码:
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>
您需要使用发送密钥。
我假设你有一个浏览按钮和一个上传按钮
driver.findElement(By.xpath("YOUR XPATH")).sendKeys("Absolute path of file");
随意更改上面代码中元素的定位器
sendkeys 将在 HTML 中为相应的上传字段设置路径和文件名
现在点击上传按钮。
ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();
注意:- 在发送密钥之间稍等片刻,然后点击上传按钮。帮了很多次
有关更多信息,请参阅下文 link:-
http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver
希望对您有所帮助:)
发送文件路径后,您可以使用这些代码行关闭上传 window 按 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);
}
我正在尝试使用 java 为网站编写 selenium 测试。但是,我在测试文件上传时遇到了问题。
当我点击文件上传按钮时,它会自动打开windows文件上传。我有代码可以将文件路径 ("D:\test.txt")
放入选择中。通过研究这个主题,我了解到 selenium webdriver 无法处理这个问题。所以我的问题是:有什么方法可以自动关闭上传 window?事实上,sendKeys 正在选择 txt 文件,但 window 上传仍然没有关闭。
提前致谢
产品操作代码:
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();
}
确认地点投注代码:
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>
您需要使用发送密钥。
我假设你有一个浏览按钮和一个上传按钮
driver.findElement(By.xpath("YOUR XPATH")).sendKeys("Absolute path of file");
随意更改上面代码中元素的定位器
sendkeys 将在 HTML 中为相应的上传字段设置路径和文件名
现在点击上传按钮。
ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();
注意:- 在发送密钥之间稍等片刻,然后点击上传按钮。帮了很多次
有关更多信息,请参阅下文 link:-
http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver
希望对您有所帮助:)
发送文件路径后,您可以使用这些代码行关闭上传 window 按 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);
}