Home > Net >  How to send a DataFrame using Selenium in python
How to send a DataFrame using Selenium in python

Time:12-07

I'm trying to send the following DataFrame using Selenium into the text field of a Capital Gains Calculator website (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

So far I'm able to send each column separately but the problem is that the columns appear one underneath the other in the text field. For the calculator to work, I would need to maintain the format of the DataFrame. Is there a way to do this?

This is what I've done so far:

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

CodePudding user response:

Easy enough to send the df to the clipboard and paste w/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()

Output

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
  • Related