How to upload a file in Selenium with no text box

后端 未结 4 1972
北海茫月
北海茫月 2020-12-01 18:58

I have been looking for a solution to uploading a file in Selenium 2.

The problem is that the web element that I\'m trying to upload is usable in two ways: Drag and

4条回答
  •  日久生厌
    2020-12-01 19:48

    Unfortunately, you can't do that as of now (January 2013, Selenium 2.29.1), because Selenium doesn't support elements.

    There is a feature enhancement request for this made by the project developers themselves, it's just not yet implemented. You can star it there to move it upwards in the priority list.

    Also, as far as I know, you can't really drag a file from desktop to a WebElement in a reliable way.

    A workaround might be the use of AutoIT (Windows only) or the Robot class (will also work only on setups similar to yours) and type the path "blindly" into the dialog:

    driver.findElement(By.id("up-drop-zone-input")).click();
    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_C);        // C
    r.keyRelease(KeyEvent.VK_C);
    r.keyPress(KeyEvent.VK_COLON);    // : (colon)
    r.keyRelease(KeyEvent.VK_COLON);
    r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
    r.keyRelease(KeyEvent.VK_SLASH);
    // etc. for the whole file path
    
    r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end
    r.keyRelease(KeyEvent.VK_ENTER);
    

    It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny KeyEvent#getExtendedKeyCodeForChar() which does similar work, but is available only from JDK7).

提交回复
热议问题