这是我的代码
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).click();
Thread.sleep(2000);
StringSelection ss= new StringSelection("C:\\Users\\ns10\\Desktop\\download.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
Robot robo=new Robot();
robo.delay(1000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
当我在Eclipse中运行上面的代码时,它会点击文件上传;然后弹出窗口,但不选择我提到的文件。它只是闲着。
有人可以帮我解决这个问题。
答案 0 :(得分:0)
如果可能的话首先尝试,
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).sendKeys("C:\\Users\\ns10\\Desktop\\download.jpg");
这是目前通过selenium webdriver上传文件的一种着名方式,您无需费心点击文件上传按钮,然后使用机器人类上传图片。只需发送要上传的文件路径即可完成上传。
如果仍然无效,则下面是使用 Robot和Sikuli(合并)完成任务的替代方法: -
有时,Robot类会将“attachment_path”键入文本框的“文件名”字段,但之后不会按“Enter键”以上传文件。
为了避免这种情况,我已经使用 Sikuli点击“打开”按钮,然后确实上传文件;
(注意: - 您必须在单击“文件上传”按钮后出现的窗口对话框中截取Open button的屏幕截图。
首先下载sikuli-api standalone jar。并将其添加到项目的构建路径中。然后,为文件上传添加以下代码: -
//Clicking on the File Upload button
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).click();
//Copying the path of the file to the clipboard
StringSelection attachment = new StringSelection("C:\\Users\\ns10\\Desktop\\download.jpg"); //path to the attachment
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(attachment, null);
//Pasting the contents of clipboard in the field "File name" of the Window Pop-up
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
try{
ScreenRegion screen = new DesktopScreenRegion(); //screen is the default screen region which corresponds to the entire screen on your default monitor.
Target target = new ImageTarget(new File("<PUT HERE THE PATH FOR THE OPEN BUTTON'S IMAGE>"));
ScreenRegion screen_web = screen.wait(target,5000);
screen_web = screen.find(target); //screen_web holds the screen region occupied by the "Open" button that was found by Sikuli's image recognition engine.
if(screen_web != null)
{
Mouse mouse = new DesktopMouse(); // Create a mouse object
mouse.click(screen_web.getCenter()); // Use the mouse object to click on the center of the target region, i.e., the Open button
}else{
throw new Throwable();
}
}catch(Throwable e){
System.err.println("The Open file button wasn't clicked!!" + e.getMessage() + "\n---------------------");
}
在Tiny Pic.com上传的代码: -
package pack_ads;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.api.DesktopScreenRegion;
import org.sikuli.api.ImageTarget;
import org.sikuli.api.ScreenRegion;
import org.sikuli.api.Target;
import org.sikuli.api.robot.Mouse;
import org.sikuli.api.robot.desktop.DesktopMouse;
public class Testing_TinyPicUpload {
public static void main(String[] args) throws AWTException, InterruptedException {
WebDriver driver = new FirefoxDriver(); //Opens a firefox browser instance
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Timeout of 30 seconds given
driver.manage().window().maximize(); //Maximizing the window
//Navigating to tiny pic site
driver.get("http://tinypic.com/");
//Uploading photo
driver.findElement(By.xpath("//input[@id='the_file']")).click(); //Locating the attach button
//Copying the path of the file to the clipboard
StringSelection photo = new StringSelection("D:\\\\Images\\default.jpg"); //Putting the path of the image to upload
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(photo, null);
//Pasting the contents of clipboard in the field "File name" of the Window Pop-up
Thread.sleep(5000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
try{
ScreenRegion screen = new DesktopScreenRegion(); //screen is the default screen region which corresponds to the entire screen on your default monitor.
Target target = new ImageTarget(new File("Tinyimages\\Open_file.png")); //Made a folder in my project as Tinyimages and put the image of Open_file in there.
ScreenRegion screen_web = screen.wait(target,5000);
screen_web = screen.find(target); //screen_web holds the screen region occupied by the "Open" button that was found by Sikuli's image recognition engine.
if(screen_web != null)
{
Mouse mouse = new DesktopMouse(); // Create a mouse object
mouse.click(screen_web.getCenter()); // Use the mouse object to click on the center of the target region, i.e., the Open button
}
else{
throw new Throwable();
}
System.out.println("Attachment was successfully done.\n------------------------------\n");
}catch(Throwable e){
System.err.println("The Open file button wasn't clicked!!" + e.getMessage() + "\n------------------------------");
}
}
}
注意:您可以从here下载使用此图片作为“打开”按钮。在代码Target target = new ImageTarget(new File("Tinyimages\\Open_file.png"));
答案 1 :(得分:0)
我找到了相同的结果,代码会在文件上传窗口打开时停止并等待,并且只会在窗口关闭后继续。
作为一种解决方法,我将线程睡眠和机器人按键放在我单击文件上传按钮之前生成的单独线程中。这解决了我的问题。
//要实例化并在按钮点击之前运行的线程类: //
public class PasteUploadFileThread扩展Thread {
String uploadFileName = "";
public PasteUploadFileThread(String filename){
this.uploadFileName = filename;
}
public void run() {
try {
// Pause 3 seconds giving window time to open up
Thread.sleep(3000);
System.out.println("STARTING PasteUploadFileThread for file: " + this.uploadFileName);
StringSelection ss= new StringSelection(this.uploadFileName);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
Robot robo=new Robot();
robo.delay(2000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_CONTROL);
robo.delay(2000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
System.out.println("ENDING PasteUploadFileThread for file: " + this.uploadFileName);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
}
//产生线程的测试类的一部分,然后单击按钮
String filepath = "C:\\UploadDocument.txt";
WebElement browseButton = wait60Secs.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(.,'Browse')]")));
// Spawn a thread that will do the interacting with the File Upload window.
// This is needed because when we click on the browse button the code halts waiting for the File Upload window to be closed.
// So this thread will go off on it's own and do it's work while this code is paused.
(new PasteUploadFileThread(filepath)).start();
browseButton.click();
// Execution continues here after the Upload window is closed...
// Find the file that was uploaded before continuing
wait60Secs.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(.,'UploadDocument.txt')]")));