【问题标题】:How to upload files to a website. using python如何将文件上传到网站。使用蟒蛇
【发布时间】:2020-02-01 10:45:16
【问题描述】:

它正在创建一个程序,自动将文件上传到 VirusTotal 站点以接收测试结果。

使用 Python Selenium,Crawling 成功。但是,上传文件失败。

如何自动上传文件? 我很抱歉我缺乏英语技能。

ps。我想用 Python 而不是 php 来解决这个问题。我不太了解Php。但是如果我必须使用php,我会学习。

【问题讨论】:

  • "使用 Python Selenium,爬取成功。但是,上传文件失败。"我们需要查看您尝试上传文件的代码,否则我们无法告诉您它有什么问题。
  • 哦,我知道了。我将再次发布包含代码的帖子。谢谢你的建议。

标签: php python file-upload web-crawler


【解决方案1】:

在 Selenium 中使用 send_keys:

element = driver.find_element_by_xpath(upload file xpath)
# maybe like this: //input[@type="file"]
element.send_keys("your file path")

如果你使用 Firefox,你应该首先让 'input' 元素可见。


您可以使用显式等待来等待元素直到可见。

from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, 'your xpath')))

【讨论】:

  • 发生错误。这是什么?? Traceback(最近一次调用最后一次):文件“我的程序路径”,第 10 行,在 file_input = browser.find_element_by_xpath('full upload file xpath')
  • 文件 "C:\Users**我的名字**\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第 394 行,在 find_element_by_xpath 中返回 self.find_element(by=By.XPATH, value=xpath)
  • 文件 "C:\Users**我的名字**\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第 978 行,在 find_element 'value': value})['value']
  • 文件 "C:\Users**我的名字**\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第 321 行,在执行 self.error_handler.check_response(response)
  • 文件 "C:\Users**我的名字**\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py",第 242 行,在 check_response raise exception_class(message, screen, stacktrace)
猜你喜欢
  • 2011-03-27
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2011-03-02
  • 2010-09-28
相关资源
最近更新 更多