Skip to content

Commit 2af378f

Browse files
committed
Fixed all of the last known bugs
1 parent 232fd31 commit 2af378f

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .api import CFD, Invest
2-
from .utils import Panel
2+
from .utils import BuyStockMethod, Panel

api.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from selenium.webdriver.support import expected_conditions
66
from 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

1111
class 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)

utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from time import sleep
22

3+
34
class Panel:
45
"""
56
Allows you to select between practice and real
@@ -11,6 +12,10 @@ class Mode:
1112
Invest = "Invest"
1213
CFD = "CFD"
1314

15+
class BuyStockMethod:
16+
Shares = "Shares"
17+
Value = "Value"
18+
1419
def force_click(element, sleep_time=1):
1520
"""
1621
Clicks something until it works
@@ -30,4 +35,4 @@ def type_sleep(element, text, time):
3035
sleep(time)
3136

3237
def script_click_xpath(driver, xpath):
33-
driver.execute_script(f"document.evaluate(\"{xpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()")
38+
driver.execute_script(f"document.evaluate(\"{xpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()")

0 commit comments

Comments
 (0)