5

I have to upload file (drop zone -> click ->open window to choose file)

I've tried:

addphoto.send_keys("C:\\files\\file.jpg") 

But it doesn't work. Is there any robot to handle with new window opened?

6
  • What is drop zone, what is addphoto?
    – Andersson
    CommentedOct 27, 2016 at 10:24
  • drop zone is the area where I click to open a window where I have to choose file. I use addphoto = driver.find_element_by_xpath("html/body/div[1]/div[1]/section[2]/div/div[1]/div/form/div[1]/div[1]/div/div[1]/div[1]/img") addphoto.click()
    – Basia
    CommentedOct 27, 2016 at 10:32
  • I'm quite sure that your <img> element cannot get path to file. Provide HTML for dropzone
    – Andersson
    CommentedOct 27, 2016 at 10:36
  • <div class="form-group"> <div id="uploader" class="col-md-6"> <div id="upload-file" class="col-md-6" style="z-index: 1;"> <div class="img-wrapper"> <img id="final-photo" class="img-responsive" alt="photo" src="placehold.it/1024x768"> </div> <div class="information"> </div> <div id="html5_1b02rhkr71un31ndt12dkm171l563_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 15px; width: 282px; height: 216px; overflow: hidden; z-index: 0;"> </div> <input id="photo" type="hidden" name="photo"/> </div>
    – Basia
    CommentedOct 27, 2016 at 10:39
  • stackoverflow.com/questions/11256732/… Here someone ask the same.CommentedOct 27, 2016 at 10:40

4 Answers 4

6

Putting a file name in dropzone hidden input works fine. This should get you going.

upload_file = driver.find_element_by_css_selector('.dz-hidden-input') data_file = Path(__file__).parent / "test_file.txt" logging.debug("data_file: %s", data_file) assert data_file.exists() upload_file.send_keys(str(data_file)) assert driver.find_element_by_css_selector('.dz-image').is_displayed() 
0
    3

    I did it! Just pip install -U pyautoit

    then import autoitautoit.win_wait_active("File Upload", 5)autoit.send(os.path.join("path"))autoit.send("{ENTER}")

    Works ok :)

      2

      Generally, no.

      Selenium can only operate your web browser. When you click any kind of an element that opens a file browser window, this window is provided by your operating system, not the web browser. This is why you can't interact with it in selenium.

      IF your web page accepts drag and drop, you might be able to hoodwink it by using sendkeys to send something like file://path/to/your/file as this is what drag and drop actually does, and then using action chains to move your mouse to the element and perform a "drop" by sending the element a release button event. See for example Unable to perform click action in selenium python

      for ideas how to use action chains.

      This is notoriously unreliable, though. If you are planning to automate posts to, say social media sites, you are probably out of luck, as their upload mechanisms are somewhat more complicated to prevent spamming using robots.

      You might want to investigate tools that allow controlling the whole GUI of your computer instead of just the browser. You could then use Selenium to try to locate the absolute position of the drag and drop field, and feed this to an external automator script that clicks on your image, drags it to that spot and drops it there.

      Hannu

        0

        @Janusz Skonieczny's answer worked fine for me. You will need the current webdriver for that solution to work. You can get it as follows if you dont have the variable on hand.

        from robot.libraries.BuiltIn import BuiltIn def get_webdriver_instance(): se2lib = BuiltIn().get_library_instance('SeleniumLibrary') #'or Selenium2Library' return se2lib.driver 

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.