2

I'm using python and selenium chrome driver to click on an upload file button which opens an open file window as shown:

enter image description here

My code uploads the files without interacting with the window. What I'm trying to do is close this window but am not sure how to go about doing this.

I've read various other posts with the similar problem but none I could find really gave me what I was looking for. I understand selenium can't access the window, and I've read I need a different module to interact with it. What would be the best way to do this?

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
blountdj
  • 599
  • 1
  • 11
  • 23

2 Answers2

1

In the end I was able to upload the item directly into the page by finding the upload button and sending it the file path.

driver.find_element_by_xpath("//*[@id='upl-fileInp']").send_keys("C:/Users/user/folder/file.jpg")
blountdj
  • 599
  • 1
  • 11
  • 23
  • Brilliant! 5 years later and you just saved me a load of time, thank you for taking the time to update your question with this solution, it's appreciated. – Peter F Oct 20 '22 at 12:39
0

Workaround

You need put inside element of upload the root file. Don't open the upload by button, just insert /root/of/file/file.pdf inside input upload value using Javascript. Remember, that's my workaround... haha

GIA
  • 1,400
  • 2
  • 21
  • 38
  • Thanks for your suggestion. A workaround is good for me. I'm not really sure how to go about inserting the file path using javascript. Do you have any advice on that please? The html for the button is: – blountdj May 09 '17 at 15:57
  • try this: `webdriver.executeScript("document.getElementById('upl-81494336238627').setAttribute('value', '/root/of/file/file.pdf')");` – GIA May 09 '17 at 16:14
  • Thanks @Guilherme lazzetta, I've been trying this for a while but can't get past this error: selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token The line I'm using is: driver.execute_script("document.getElementById('upl-81494350013226').setAttrib‌​ute('value', 'C:\\Users\\darren b\\Desktop\\eg.jpg');") – blountdj May 09 '17 at 17:41
  • I've now resolved the error before (not sure how - the code is the same). The value attribute have changed to the value path but the file hasn't been uploaded? – blountdj May 09 '17 at 18:13
  • Sorry if this is a dumb question but I've not used javascript before, but how do I go about submitting the form? – blountdj May 09 '17 at 18:56
  • After set value inside upload input, with selenium press button "SEND" or something like that. If don't work, send link to I make some tests to help you. – GIA May 09 '17 at 19:08
  • Thanks @Guilherme lazzetta. I wasn't able to make that work but found another way which worked for me. Thanks for your help – blountdj May 10 '17 at 14:56