55from selenium .webdriver .support import expected_conditions
66from selenium .webdriver .support .wait import WebDriverWait
77
8- from .utils import Mode , Panel , force_click , script_click_xpath , type_sleep
8+ from .utils import *
99
1010
1111class Trading212 :
@@ -93,14 +93,15 @@ def setup(self, username, password, panel):
9393 elem = self .driver .find_element_by_class_name ("account-menu-button" )
9494 elem .click ()
9595
96- def buy_stock (self , stock , amount ):
96+ def buy_stock (self , stock , amount , method = BuyStockMethod . Shares ):
9797 """
9898 Buying a stock
9999 :param stock: Stock (Display name)
100100 :param amount: It may be the amount of the shares and it may be the value of the money. It depends on your Trading212 defaults and I may fix it in the future. For now, You can debug it with headless=False parameter in the constructor.
101+ :param method: Shares - For buying the stock by its shares amount | Value - For buying the stock by its value
101102 """
102- # It's just opening a stock and buying it
103103 self .open_stock_dialog (stock )
104+ self .switch_buying_method (method )
104105 self .buy (amount )
105106 sleep (self .long_sleep )
106107
@@ -124,6 +125,17 @@ def buy(self, amount):
124125 else :
125126 self .driver .find_elements_by_xpath ("//div[contains(@class,'confirm-button')]" )[0 ].click ()
126127
128+ def switch_buying_method (self , method ):
129+ if self .mode == Mode .Invest :
130+ force_click (WebDriverWait (self .driver , self .timeout ).until (expected_conditions .element_to_be_clickable ((By .XPATH , "//div[@class='invest-by-content']" ))))
131+ if method == BuyStockMethod .Value :
132+ # item item-invest-by-items-value
133+ # item item-invest-by-items-quantity
134+ self .driver .find_element_by_xpath ("//div[@class='item item-invest-by-items-value']" ).click ()
135+ else :
136+ self .driver .find_element_by_xpath ("//div[@class='item item-invest-by-items-quantity']" ).click ()
137+
138+
127139 def open_stock_dialog (self , stock ):
128140 """
129141 Opening stock dialog
@@ -194,6 +206,7 @@ def close_position(self, stock):
194206 # God knows why selenium can't click it. So I'm using javascript hacks.
195207 quantity = self .position_info (stock , "quantity" )
196208 script_click_xpath (self .driver , f"//td[@class='name' and text()='{ stock } ']/following::div[@class='button-wrapper']/child::div[@class='sell-button']" )
209+ self .switch_buying_method (BuyStockMethod .Shares )
197210 elem = WebDriverWait (self .driver , self .timeout ).until (expected_conditions .visibility_of_element_located ((By .XPATH ,"//div[@class='visible-input']//input" )))
198211 elem .clear ()
199212 type_sleep (elem , quantity , self .short_sleep )
0 commit comments