【问题标题】:How to download a file from selenium python when calling ng-click调用ng-click时如何从selenium python下载文件
【发布时间】:2019-04-10 16:28:16
【问题描述】:

单击使用 ng-click 定义的按钮时,我正在尝试使用 Selenium Python 模拟从网站下载文件。

HTML 文件包含以下内容:

<div class="col-lg-6 btn-group">
  <br/>
  <br/>
  <input type="button" ng-click="exportToExcel('#exportable')" class="btn-text btn-link export-main" value="Export" />
</div>

检查不同选项后我输入的代码没有做任何事情。

当我使用 Selenium IDE 时,记录的唯一步骤是“单击”“css=btn-text”元素,这可以正常工作,但是使用 chrome 驱动程序和 Python 中的 Selenium 没有下载任何内容。

我有这段代码来确保 Chrome 驱动程序下载文件。 (我已经在其他网站上进行了测试,这很有效)

 options = webdriver.ChromeOptions()
          prefs = {'download.default_directory' : self.download_location,
              'download.prompt_for_download': False,
              'download.directory_upgrade': True,
              'safebrowsing.enabled': False,
              'safebrowsing.disable_download_protection': False}
          options.add_argument('--headless')
          options.add_experimental_option('prefs', prefs)
          driver = webdriver.Chrome(chrome_options=options, 
                                   service_args=['--verbose', '--log-path=/tmp    /chromedriver.log'])

这是我用来点击按钮下载文件的代码:

export_button = WebDriverWait(self.webdriver,10).until(ec.visibility_of_element_located((By.CLASS_NAME, "export-main"))) 

 ActionChains(self.webdriver)\
  .click(export_button)\
  .move_to_element(export_button)\
  .perform()

我希望将指定文件夹中的 excel 文件下载到 Chrome 驱动程序中,但 Selenium 和 Angular 似乎不能很好地配合使用。

还有其他方法可以使用 Javascript 调用“ExportToExcel”函数吗?

【问题讨论】:

  • 试试这个。看看有没有帮助export_button = WebDriverWait(self.webdriver,10).until(ec.element_to_be_clickable((By.CSS_SELECTOR, "input.export-main")))
  • 感谢您的回复。我试过了,但没有用。
  • 您遇到了什么错误?任何堆栈跟踪?
  • 要简单回答有关使用 javascript 调用 ExportToExcel 的问题,只需运行 driver.execute_script("exportToExcel('#exportable');")。我不知道这是否会下载您的文件,但这就是您在 webdriver 中执行 javascript 的方式。
  • @KunduK 我没有得到任何堆栈跟踪。没有错误。我只是从 /tmp/chromedriver.log 中的 Chrome 驱动程序获取调试语句,但我在日志中看不到任何错误(在调用时没有堆栈跟踪。有没有办法调试更多的 webdriver?

标签: javascript python angular selenium selenium-chromedriver


【解决方案1】:

我自己无法对此进行测试,因为每个网站都不同,您也许可以调用您所询问的 javascript 函数。 Selenium 允许您非常轻松地从 webdriver 中执行 javascript。这可以通过

来完成
driver.execute_script("exportToExcel('#exportable');")

我不知道这是否会下载您的文件,但这回答了您如何在 webdriver 中执行 javascript 的问题。同样,这是否会下载文件取决于许多其他情况,如果没有更多信息,则无法从您的帖子中确定。

【讨论】:

  • “exportToExcel”是一个 Angular 函数,当我输入这个命令时,它抱怨该函数不存在。以下是来自 chrome 驱动程序的日志:[1554990242.867][INFO]: [60f33b385dd98a5ff18a450760d13d7e] COMMAND ExecuteScript { "args": [ ], "script": "exportToExcel('#exportable')", "sessionId": "60f33b385dd98a5ff18a450760d13d7e" }`1554990242.870][INFO]:[60f33b385dd98a5ff18a450760d13d7e] RESPONSE ExecuteScript ERROR javascript error: exportToExcel is not defined`
  • 可能是调用时没有加载Javascript函数?如何确保加载完整的 DOM?
  • @juanv 老实说,没有任何可重现的代码,我们真的无法提供帮助。在这一点上,一切都只是猜测。您拥有用于创建驱动程序实例、定位按钮并等待按钮的代码,但我们不知道该站点还发生了什么,因为我们甚至不知道该站点是什么。
【解决方案2】:

我得到了问题的答案:

只需调用 javascript 点击“导出按钮”,如下所示:

self.webdriver.execute_script("arguments[0].click();", export_button)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2015-10-16
    • 2016-04-16
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多