Python Selenium Webdriver下载CSV

Python Selenium Webdriver下载CSV,python,csv,selenium-webdriver,webautomation,Python,Csv,Selenium Webdriver,Webautomation,我正在努力使用Selenium下载CSV文件好几天。请告知,非常感谢 我使用SeleniumWebDriver语言绑定(Python)2.4+HTMLUnit浏览器 代码: 在该网页中,如果我使用Firefox,单击“生成csv”按钮后,它将生成一个csv文件,并通常下载它。由于我使用HTMLUnit,很难实现下载文件,因此我使用page\u source属性来获取CSV内容 有时候,它是成功的!!但有时它会抛出一个错误: org.openqa.selenium.NoSuchElementExc

我正在努力使用Selenium下载CSV文件好几天。请告知,非常感谢

我使用SeleniumWebDriver语言绑定(Python)2.4+HTMLUnit浏览器

代码:

在该网页中,如果我使用Firefox,单击“生成csv”按钮后,它将生成一个csv文件,并通常下载它。由于我使用HTMLUnit,很难实现下载文件,因此我使用
page\u source
属性来获取CSV内容

有时候,它是成功的!!但有时它会抛出一个错误:

org.openqa.selenium.NoSuchElementException: Returned node was not an HTML element
有人能帮我分析一下为什么会这样吗?我很困惑,运行脚本就像掷骰子一样

多谢各位

更新:(部分回溯)


听起来在你点击generate csv按钮之前,你的html还没有完成加载。当从javascript加载html时,selenium会出现这种情况——至少对我来说是这样

不确定这是否是最好的处理方法,但我会使用递归方法单击,直到您得到它

import time
def generateCsv(browser):
    try:
        browser.find_element_by_id("generate_csv").click()
        csv_file = browser.page_source
    Except NoSuchElementException,e:
        time.sleep(3)
        generateCsv(browser)

希望有帮助

您能展示完整的回溯吗?谢谢。您是否尝试过使用
显式等待
@alecxe谢谢!我只是在我的问题中添加了部分回溯。+1表示“HTML未完成加载..与Selenium一起发生了一系列情况”,感谢您的回答!由于所有数据都在网页上,我最终下载了整个html文件内容,并将其解析为CSV文件。
14:29:15.913 INFO - Executing: [find element: By.selector: .controlbuttons > a > img[alt='CSV']])
14:29:16.404 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: Returned node was not an HTML element
For documentation on this error, please visit: ...
Driver info: driver.version: EventFiringWebDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByCssSelector(HtmlUnitDriver.java:952)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1565)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1241)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1562)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:530)
    at sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:101)
    at com.sun.proxy.$Proxy14.findElement(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:184)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:47)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:169)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)
14:29:16.405 WARN - Exception: Returned node was not an HTML element
import time
def generateCsv(browser):
    try:
        browser.find_element_by_id("generate_csv").click()
        csv_file = browser.page_source
    Except NoSuchElementException,e:
        time.sleep(3)
        generateCsv(browser)