如何在不使用sendkeys的情况下从窗口弹出窗口中从本地机器中选择selenium webdriver中的文件

时间:2017-08-16 10:53:37

标签: java selenium-webdriver

我需要从本地机器的selenium webdriver中选择一个文件,但由于selenium只支持web,所以我无法从本地机器(Window poup)中选择文件,因为它没有任何我可以填充的路径通过sendkeys文件的路径。是否有任何解决方案(使用机器人类或autoIT工具或任何其他解决方案),我可以从窗口中选择文件。我已经研究过autoIt工具但是没有足够的细节来实现,如果有人知道的话请回复此问题。请参阅附件截图以供参考。

Upload file from local machine window poup after clicking on choose file button

3 个答案:

答案 0 :(得分:0)

假设您在文件上传

中将文件名设为“文件”
WebElement uploadfile = driver.findElement(By.name("file"));
uploadfile.sendKeys(filepath)

driver.findElement(By.id("file")).sendKeys("C:\\path\\to\\file.txt");

其中C:\ path \ to \ file.txt是文件位置。

答案 1 :(得分:0)

只需使用type=file图片的绝对路径。

找到 WebElement button2 = mega.waitsss(driver, upload2); button2.sendKeys("C:\\Users\\user\\Desktop\\logo\\Summit-Logo-900px.png"); button2.click(); } WebElement waitsss(WebDriver driver, By elementIdentifier){ Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); return wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(elementIdentifier); } }); } 的标签,它不是强制性的,但是selenium支持此标签到uplaod文件。

还使用等待来防止脚本失败

index,country,year,Country code,Total population (Gapminder),Life satisfaction in Cantril Ladder (World Happiness Report 2017),GDP per capita
62,Afghanistan,2008,AFG,29839994.0,3.723589897,1298.14315888
63,Afghanistan,2009,AFG,30577756.0,4.401778221,1531.17399272
64,Afghanistan,2010,AFG,31411743.0,4.75838089,1614.25500126
65,Afghanistan,2011,AFG,32358260.0,3.83171916,1660.73985618
66,Afghanistan,2012,AFG,33397058.0,3.782937527,1839.27357928
67,Afghanistan,2013,AFG,34499915.0,3.572100401,1814.15582533
167,Albania,2007,ALB,3169665.0,4.634251595,8447.88228539
169,Albania,2009,ALB,3192723.0,5.485469818,9524.60981095
170,Albania,2010,ALB,3204284.0,5.268936634,9927.13514733
171,Albania,2011,ALB,3215988.0,5.867421627,10207.7006745
172,Albania,2012,ALB,3227373.0,5.510124207,10369.7616592
173,Albania,2013,ALB,3238316.0,4.550647736,10504.0930888
242,Algeria,2010,DZA,35468208.0,5.46356678,12870.2162376
243,Algeria,2011,DZA,35980193.0,5.317194462,12989.9549601
244,Algeria,2012,DZA,36485828.0,5.604595661,13161.566464
451,Angola,2011,AGO,19618432.0,5.589000702,5911.25433387
452,Angola,2012,AGO,20162517.0,4.360249996,5998.63860099
453,Angola,2013,AGO,20714494.0,3.937106848,6185.0138292

答案 2 :(得分:0)

我使用过Robot类,这比使用AutoIT工具要容易得多。请找到以下代码:

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

    public void testUpload() throws InterruptedException
        {
            WebElement element = driver.findElement(By.name("uploadfilebutton"));
//can use By cssSelector or name (path) as per convenience
            element.click();
            uploadFile("path to the file");
            Thread.sleep(2000);
        }

        /**
         * This method will set any parameter string to the system's clipboard.
         */
        public static void setClipboardData(String string) {
            //StringSelection is a class that can be used for copy and paste operations.
               StringSelection stringSelection = new StringSelection(string);
               Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
            }

        public static void uploadFile(String fileLocation) {
            try {
                //Setting clipboard with file location
                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);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }