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?

2
  • Why do you need to click on that upload file button? Commented May 9, 2017 at 15:38
  • This solution might be viable as a workaround: stackoverflow.com/a/10472542/563231 Commented May 9, 2017 at 15:39

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

7 Comments

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: <input class="upl-fileInp" type="file" name="d" value="" title="Add photos" multiple="" id="upl-81494336238627">
try this: webdriver.executeScript("document.getElementById('upl-81494336238627').setAttribute('value', '/root/of/file/file.pdf')");
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');")
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?
Sorry if this is a dumb question but I've not used javascript before, but how do I go about submitting the form?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.