python - Selenium Webdriver 下载 CSV

标签 python csv selenium-webdriver webautomation

这几天我一直在努力使用 Selenium 下载 CSV 文件。请指教,不胜感激!!

我使用 Selenium Webdriver Language Bindings (Python) 2.4 + HTMLUnit 浏览器。

代码:

browser.find_element_by_id("generate_csv").click()
csv_file = browser.page_source

在该网页中,如果我使用 Firefox,单击“generate_csv”按钮后,它会生成一个 CSV 文件,并且通常会下载它。由于我使用的是HTMLUnit,很难实现下载文件,所以我使用page_source属性来获取CSV内容。

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

org.openqa.selenium.NoSuchElementException: Returned node was not an HTML element

谁能帮我分析一下为什么会这样?我很困惑,运行脚本就像掷骰子。

谢谢。

更新:(回溯的一部分)

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

最佳答案

听起来您的 html 在您点击生成 csv 按钮之前没有完成加载。当从 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)

希望对你有帮助

关于python - Selenium Webdriver 下载 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24544118/

相关文章:

Python:webdriver Firefox 和 PhantomJS 之间的区别问题

python - 在 Python 中查找与条件匹配的最后一项

python - 在 Python 中,为什么我的 for 循环只在特定数字之前的数字是数组中的最后一个时才排除该数字?

python - 如何解决 'django_content_type already exists' ?

python - 使用 PyUSB 发送 HID 报告

Python 2.7 CSV文件读/写\xef\xbb\xbf代码

php - 导出到 Excel 或 csv

python - 在 Python 中比较黄金标准 csv 文件和提取值 csv 文件

java - 无法点击div标签元素

java - 如何克服异常——在java中发送电子邮件时字符串 ``中的非法地址