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,234
  • 12
  • 59
  • 81
Prince
  • 25
  • 1
  • 6
  • Possible duplicate of [Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome) – undetected Selenium May 03 '18 at 12:51

1 Answers1

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

Deb
  • 2,922
  • 1
  • 16
  • 32
  • Awesome Man! I didn't notice that, my bad ! Thanks a lot for the prompt answer ! – Prince May 03 '18 at 14:24
  • I am glad it was helpful. If you think it helped please mark the answer as accepted. Thanks – Deb May 03 '18 at 15:09