Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用python和selenium在网站中上载文件_Python_Selenium_Selenium Webdriver_Automation - Fatal编程技术网

无法使用python和selenium在网站中上载文件

无法使用python和selenium在网站中上载文件,python,selenium,selenium-webdriver,automation,Python,Selenium,Selenium Webdriver,Automation,我想自动化网站上的文件压缩。我使用selenium上传文件,但没有这样做。 下面是代码 file_path = "/home/gugli/Documents/script_py/Dainik_Jagron/h2.pdf" browser = webdriver.Firefox() url = 'http://pdfcompressor.com/' browser.get(url) 我尝试插入输入标记,但出现错误 browser.find_element_by_id('html5_1cciqvn

我想自动化网站上的文件压缩。我使用selenium上传文件,但没有这样做。 下面是代码

file_path = "/home/gugli/Documents/script_py/Dainik_Jagron/h2.pdf"
browser = webdriver.Firefox()
url = 'http://pdfcompressor.com/'
browser.get(url)
我尝试插入输入标记,但出现错误

browser.find_element_by_id('html5_1cciqvn90sehe7rachs1c3m03').send_keys(filepath)
以下是错误:

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"]
该文件存储在ul标签中。但在此标签中甚至未能上载。 下面是上载文件前后的dom结构快照

上载的文件存储为li元素图像。我尝试在div id=carousel容器中插入,但再次尝试失败。
我还可以用python在这里上传文件。

从您的url看,selenium似乎找不到元素

selenium.common.exceptions.NoSuchElementException:

Message: Unable to locate element: [id="html5_1cciqvn90sehe7rachs1c3m03"]
在这种特定情况下,输入字段的id是自动生成的,这意味着每个会话的id都不同。您看到的id与通过selenium打开时不同

在这种特殊情况下,我建议通过XPath而不是id来定位元素


使用XPath//输入[type='file']或其他一些东西,以便selenium能够识别元素

可能重复的awesomeman!我没注意到,我的坏朋友!非常感谢您的及时回复!我很高兴这是有益的。如果您认为这有帮助,请将答案标记为已接受。谢谢