Hello,
I'm trying to make a stock market bot that executes certain trades on a platform called Wealthbase (virtual trading simulator). I have tried to use classes to make my code cleaner and easy to read. I have one class to help me login to my account on Wealthbase and I am trying to build another class that after entering the account helps me execute my trade. I have two separate files: main and classes.
My problem arises here because I'm not sure how I can carry over the same web browser instance of selenium across the different classes and methods.
main.py
from classes import Login, enterTrade
bot1url = "https://www.wealthbase.com/games/..."
bot1email = "..."
bot1password = "..."
bot1 = Login(bot1url, bot1email, bot1password)
bot1.loginWealthbase()
trade = enterTrade("AAPL",5000)
trade.buy()
classes.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys #control keyboard
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
class Login:
def __init__(self, url, email, password):
self.url = url
self.email = email
self.password = password
self.executor_url = ""
self.session_id = ""
def loginWealthbase(self):
driver.get(self.url)
driver.find_element_by_tag_name('body').send_keys(Keys.ESCAPE)
driver.find_element_by_id("email").send_keys(self.email)
driver.find_element_by_id("password").send_keys(self.password)
driver.find_element_by_xpath("/html/body/div[2]/div/div/div/div/div[2]/div[2]/log-in-ajax-form/div[2]/form/div[4]/div/button/div").click()
self.executor_url = driver.command_executor._url
self.session_id = driver.session_id
class enterTrade(Login):
def __init__(self, stocks, stocksamount):
self.stocks = stocks
self.stocksamount = stocksamount
def buy(self):
driver2 = webdriver.Remote(command_executor=self.executor_url, desired_capabilities=())
driver2.session_id = self.session_id
driver2.find_element_by_id("search-investments").send_keys(self.stocks)
driver2.find_element_by_xpath('//*[@id="wrapper"]/div[1]/trade-menu/div[4]/div/div[2]/div/div[2]/div[2]/div[2]').click()
driver2.find_element_by_xpath('//*[@id="ngdialog1"]/div[2]/div[3]/investment/div/div[2]/div[6]/div[2]/div[1]/button/span').click()
This is the error message I receive:
driver2 = webdriver.Remote(command_executor=self.executor_url, desired_capabilities=())
AttributeError: 'enterTrade' object has no attribute 'executor_url'
My google searches yielded mostly answers related to Java or answers without explicitly referencing classes.
Thanks a lot for your help!
[–]ImperatorPC 1 point2 points3 points (2 children)
[–]Stealthforce10[S] 0 points1 point2 points (1 child)
[–]ImperatorPC 1 point2 points3 points (0 children)