我正在尝试使用selenium发送密钥上传文件,但不能正常工作,。在我的情况下,按钮名称为Attach Sign Off,它不适用于它。请帮忙
<form class="v-upload v-widget v-upload-immediate" enctype="multipart/form-data" method="post" action="https://gbl04115.systems.uk.hsbc:8571/DSLWeb/APP/UPLOAD/2/921/action/3305f203-9e0c-4213-aecd-6ee2b2b29eb1" target="921_TGT_FRAME">
<div aria-describedby="gwt-uid-2">
<input type="hidden"/>
<input class="gwt-FileUpload" type="file" name="921_file" aria-describedby="gwt-uid-2"/>
<div class="v-button" tabindex="0" role="button" aria-hidden="false" aria-describedby="gwt-uid-2">
<span class="v-button-wrap">
<span class="v-button-caption">Attach Sign-off</span>
</span>
答案 0 :(得分:0)
我希望这个答案有助于解决您的问题。但我还没有测试过。
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
(OR)
driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");
答案 1 :(得分:0)
尝试这个并让我知道它是否无效
WebElement fileInput = driver.findElement(By.className("gwt-FileUpload"));
fileInput.sendKeys("C:/path/to/file.jpg");
答案 2 :(得分:0)
@AutoMater - 您可以按以下方式更新代码:
它应该起作用,因为它对我有用。并根据您的要求优先考虑您的测试方法。仅举例来说,我在此优先考虑@Test(优先级= 1)。我希望它适合你。
@Test(priority = 1)
public void CERTIFICATIONSSCREENUploadCertficationFilesValidation()
throws InterruptedException, AWTException {
//Click on File Upload Button
driver.findElement(By.xpath("//*[@id='certificationFile']")).click();
Thread.sleep(1000);
// Set the file name in the clipboard. Also following line of code will search file in your computer so make sure you provide correct file path.
StringSelection s = new StringSelection("C:\\Doc\\CertificationFile.xls");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
Thread.sleep(1000);
Robot robot1 = new Robot();
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
robot1.keyPress(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
}
答案 3 :(得分:0)
List elements = driver.findElements(By.className(&#34; gwt-FileUpload&#34;)); elements.get(1).sendKeys(文件路径);
使用上面的代码。 谢谢@Andersson