简体   繁体   中英

Unable to upload file in a website using python and selenium

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM