[英]Upload file using webdriver
I have tried to upload a photo using the code 我尝试使用代码上传照片
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");
But the image is not getting uploaded. 但是图像没有被上传。
The html of the upload button is 上传按钮的html是
<button id="upfile1" class="buttonclass" style="cursor: pointer" type="button"> Choose Photo</button>
Is there any other way to upload image. 还有其他上传图片的方法。 I have tried using WebElement also.
我也尝试过使用WebElement。 I need a solution in JAVA.
我需要使用JAVA解决方案。
I was able to do it using 我能够使用
driver.findElement(By.id("upfile1")).click();
Thread.sleep(2000);
StringSelection ss = new StringSelection("C:\\Users\\logo1.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
But is there any other simple methods to achieve the same other than using robot? 但是,除了使用机器人之外,还有其他简单的方法可以实现相同目的吗?
Add System.Windows.Forms reference and try the below code. 添加System.Windows.Forms参考,然后尝试以下代码。
driver.findElement(By.Id("upfile1")).Click();
System.Windows.Forms.SendKeys.SendWait("C:\\Users\\path\\ben.jpg");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
出现的弹出窗口是一个Windows窗口,应使用AutoIT处理。
If you are using RemoteWebDriver
, then you need to use LocalFileDector
to upload the file to the remote selenium server first. 如果使用
RemoteWebDriver
,则需要使用LocalFileDector
首先将文件上传到远程RemoteWebDriver
服务器。 And then use the remote path to upload from the remote selenium server. 然后使用远程路径从远程Selenium服务器上载。
driver.setFileDetector(new LocalFileDetector());
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.