如果它们不是文本区域,则使用Selenium WebDriver(java)上载文件以发送文件路径 [英] Uploading file using selenium WebDriver (java) if their is no text-area to send file path

查看:102
本文介绍了如果它们不是文本区域,则使用Selenium WebDriver(java)上载文件以发送文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Selenium WebDriver在"dropbox.com"中上传文件,当我单击选择文件"时,它会自动弹出一个窗口,它们没有文本区域可发送文件路径.如何处理这种情况? 在某些情况下,人们使用Robot类,但我不了解它的用途,我阅读了Java文档,但我没有明确的主意,请问对此有何帮助,非常感谢.

I was trying to upload a file in "dropbox.com" using selenium WebDriver, when i click on choose file it pops up a window automatically, their is no text area to send file path. how to handle this kind of situation? and in some situation people use Robot class but i did not understand what is the use of it, i read in java documentation but i did not get clear idea, could some one help on this please, help is greatly appreciated.

推荐答案

1.使用sendKeys():

如果为input type = file,则可以轻松使用sendKeys()方法并将文件的绝对路径传递给Choose file控件.

If the input type = file then you can easily use the sendKeys() method and pass the file's absolute path to the Choose file control.

//Find the element of upload button and send the path
WebElement element= driver.findElement(By.name("datafile"));
element.sendKeys("C:\Users\Easy\Desktop\testfile.txt");

2.使用AutoIT:

由于文件上载窗口是Windows控件,因此您将不得不使用Windows自动化工具(如AutoIT)来发送文件路径.使用AutoIT,您可以创建.au3文件并将其编译为.exe,然后从Selenium脚本中调用该exe文件.

Since the file upload window is a Windows control, you will have to use a Windows automation tool like AutoIT to send your file path. With AutoIT you can create an .au3 file and compile it to a .exe and invoke this exe file from your Selenium script.

Runtime.getRuntime().exec("your exe file");

AutoIT代码为:

And the AutoIT code would be:

WinWaitActive("File Upload") 
Send("Full path of the document")
Send("{ENTER}")

3.使用机器人课程:

使用Robot类,您可以将文件的绝对路径复制到剪贴板,然后将其粘贴到Windows文件上传窗口中.

Using Robot class, you can copy the file's absolute path to your clipboard and then paste it into the Windows File upload window.

setClipboardData(fileLocation);
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

这篇关于如果它们不是文本区域,则使用Selenium WebDriver(java)上载文件以发送文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆