0

I want to automate file compression on " http://pdfcompressor.com/ " website. I used selenium to upload files but failed to do so. Below is the code

file_path = "/home/gugli/Documents/script_py/Dainik_Jagron/h2.pdf"
browser = webdriver.Firefox()
url = 'http://pdfcompressor.com/'
browser.get(url)

I tried inserting in the input tag but got an err

browser.find_element_by_id('html5_1cciqvn90sehe7rachs1c3m03').send_keys(filepath)

this is the snapshot of input tag

following is the err :

Traceback (most recent call last):
 File "/home/gugli/Documents/script_py/Dainik_Jagron/uploadfiles.py", line 32, in <module>
 browser.find_element_by_id('html5_1cciqvn90sehe7rachs1c3m03').send_keys("/home/gugli/Documents/script_py/Dainik_Jagron/h2.pdf")
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 330, in find_element_by_id
 return self.find_element(by=By.ID, value=id_)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element
 'value': value})['value']
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
 self.error_handler.check_response(response)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException:
Message: Unable to locate element: [id="html5_1cciqvn90sehe7rachs1c3m03"]

The file is stored in a "ul" tag. But failed to upload in this tag even. Here is the snapshot of dom structure for before and after uploading the file

Before uploading file

After uploading file

The file uploaded is stored as a "li" element ( image 3 ). I tried inserting in "div id = carousel" container but again it was a failed attempt. How else can I upload file here using python.

Nishant Nawarkhede
8,40813 gold badges61 silver badges83 bronze badges
asked May 3, 2018 at 12:34
1

1 Answer 1

1

From your url it seems the element not found by the selenium

selenium.common.exceptions.NoSuchElementException:
Message: Unable to locate element: [id="html5_1cciqvn90sehe7rachs1c3m03"]

In this specific case the id of input field is auto generated, it means it will be different for every session. What you see as an id is different than when you open through selenium.

I would suggest to locate the element by XPath not by id in this particular case

Use XPath .//input[type = 'file'] or something other so that selenium can identify the element

answered May 3, 2018 at 12:46
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome Man! I didn't notice that, my bad ! Thanks a lot for the prompt answer !
I am glad it was helpful. If you think it helped please mark the answer as accepted. Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.