简体   繁体   English

调用ng-click时如何从selenium python下载文件

[英]How to download a file from selenium python when calling ng-click

I am trying to emulate to download a file from a website using Selenium Python when clicking a button that is defined with ng-click. 我正在尝试模拟使用Selenium Python从网站下载文件时单击使用ng-click定义的按钮。

The HTML file has this content: 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>

The code that I put after checking different options is not doing anything. 检查不同选项后我放的代码没有做任何事情。

I 一世

When I use the Selenium IDE, the only step recorded is to "click" on the "css=btn-text" element and that works, but with the chrome driver and Selenium in Python nothing is being downloaded. 当我使用Selenium IDE时,记录的唯一步骤是“点击”“css = btn-text”元素,这是有效的,但是在Python中使用chrome驱动程序和Selenium,没有任何内容被下载。

I have this code to make sure that Chrome Driver downloads a file. 我有此代码以确保Chrome驱动程序下载文件。 (I have tested with other sites and this works) (我已经与其他网站一起测试过,这项工作)

 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'])

Here is the code that I am using to click on the button to download the file: 这是我用来点击按钮下载文件的代码:

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()

I would expect to download the excel file in the designated folder to the Chrome Driver, but it seems that Selenium and Angular don't play well together. 我希望将指定文件夹中的excel文件下载到Chrome驱动程序,但似乎Selenium和Angular不能很好地协同工作。

Is there other way to call the "ExportToExcel" function using Javascript? 有没有其他方法使用Javascript调用“ExportToExcel”函数?

not being able to test this myself, since every website is different, you might be able to call the javascript function you are asking about. 我自己无法测试,因为每个网站都不同,你可以调用你要问的javascript函数。 Selenium allows you to execute javascript from within webdriver pretty easily. Selenium允许您轻松地从webdriver中执行javascript。 this can be done with 这可以用来完成

driver.execute_script("exportToExcel('#exportable');")

I don't know if this will download your file, but that answers your question of how you execute javascript within webdriver. 我不知道这是否会下载你的文件,但这回答了你如何在webdriver中执行javascript的问题。 again, whether or not this will download a file depends on many other circumstances that can't be determined from your post without having more information. 再次,这是否会下载文件取决于许多其他情况,如果没有更多信息,无法从您的帖子确定。

I got the answer to the problem: 我得到了问题的答案:

just call the javascript to click on the "export button" like this: 只需调用javascript点击“导出按钮”,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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