【问题标题】:How to send a DataFrame using Selenium in python如何在 python 中使用 Selenium 发送 DataFrame
【发布时间】:2022-01-11 05:01:04
【问题描述】:

我正在尝试使用 Selenium 将以下 DataFrame 发送到资本收益计算器网站 (http://www.cgtcalculator.com/calculator.aspx) 的文本字段中。

   B  01/12/2000  BARC   3000   4.82  15    72.3
   B  02/12/2000  BARC   5000  4.825  15  120.62
   B  03/09/2002   VOD  18000  3.040  10  273.60
   B  15/01/2003   BP.   5000  3.750  10   93.75
   B  24/03/2003   BP.   3000  3.820  10   57.30
   S  14/04/2003  BARC   6000  5.520  15    0.00
   S  23/02/2004   VOD   9000  3.620  10    0.00
   S  24/02/2004   VOD   9000  3.625  10    0.00
   S  15/07/2005   BP.   8000  6.280  10    0.00
   B  22/01/2007   BP.   5000  5.500  10  124.20
   B  22/06/2009   BP.   2000  5.020  10   50.20
   S  24/12/2012   BP.   5000  4.336  10    0.00

到目前为止,我能够单独发送每一列,但问题是这些列在文本字段中出现在另一列之下。为了让计算器工作,我需要保持 DataFrame 的格式。有没有办法做到这一点?

这是我到目前为止所做的:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd

df = pd.read_csv("/Users/Test.csv")

driver = webdriver.Chrome()
driver.get("http://www.cgtcalculator.com/calculator.aspx")

inputElement = driver.find_element_by_id("TEXTAREA1")
inputElement.send_keys("\n".join(df. iloc[:, 0]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 1]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 2]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 3].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 4].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 5].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 6].astype(str)),"\n")
driver.find_element_by_id("Button3").click()

driver.find_element_by_id("Button1").click()

【问题讨论】:

    标签: python pandas selenium


    【解决方案1】:

    很容易将 df 发送到剪贴板并使用 selenium 粘贴

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import pandas as pd
    
    df = pd.DataFrame({'B': ['B', 'B', 'B', 'B', 'S', 'S', 'S', 'S', 'B', 'B', 'S'],
     '01/12/2000': ['02/12/2000',  '03/09/2002',  '15/01/2003',  '24/03/2003',  '14/04/2003',  '23/02/2004',  '24/02/2004',  '15/07/2005',  '22/01/2007',  '22/06/2009',  '24/12/2012'],
     'BARC': ['BARC',  'VOD',  'BP.',  'BP.',  'BARC',  'VOD',  'VOD',  'BP.',  'BP.',  'BP.',  'BP.'],
     '3000': [5000, 18000, 5000, 3000, 6000, 9000, 9000, 8000, 5000, 2000, 5000],
     '4.82': [4.825, 3.04, 3.75, 3.82, 5.52, 3.62, 3.625, 6.28, 5.5, 5.02, 4.336],
     '15': [15, 10, 10, 10, 15, 10, 10, 10, 10, 10, 10],
     '72.3': [120.62, 273.6, 93.75, 57.3, 0.0, 0.0, 0.0, 0.0, 124.2, 50.2, 0.0]})
    
    driver = webdriver.Chrome()
    driver.get("http://www.cgtcalculator.com/calculator.aspx")
    
    inputElement = driver.find_element_by_id("TEXTAREA1")
    
    df.to_clipboard(index=False, sep='\t')
    
    inputElement.send_keys(Keys.CONTROL, 'v')
    
    driver.find_element_by_id("Button1").click()
    

    输出

    SUMMARY INFORMATION
    
    Year    CapitalGain      Exemption    UsePrevLosses   TaperRelief   ChargeableGain      Tax*
    -------------------------------------------------------------------------------------------
    03-04   14175         7900      0       0       6275        1059
    05-06   19848         8500      0       0       11348       2060
    12-13   -5284         10600     0       0       0       0
    

    【讨论】:

    • 非常感谢您花时间回答我的问题,但是,当我运行您的代码时,网页的文本字段中似乎没有粘贴任何数据。 df.to_clipboard 之后是否有一行丢失?谢谢!
    • 好的,刚刚意识到它是 Mac 上的 inputElement.send_keys(Keys.COMMAND, 'v') - 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多