0

I am building a stock trading bot for fun and buying/selling on a stock trading simulator. I have all the webscraping done, all the send_keys done. I just want to be able to execute multiple lines of code as one simple command instead of having to repeat code over and over, making the program really long. For example, if I want to buy a stock, I have to execute all this code for it to complete the buy order:

driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[1]/div/div[1]/input').click()     
driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN) 
time.sleep(1)       
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/table/tbody/tr/td[2]/a/span').click() 
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="shares"]').click() 
driver.find_element_by_xpath('//*[@id="shares"]').clear() 
driver.find_element_by_xpath('//*[@id="shares"]').send_keys('0.01') 
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[1]/form/div[3]/div/button[3]').click() 

Im pretty new at this and I know this wont work but, can I do something like:

    Buy = driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/input').click()     
    driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN) 
    time.sleep(1)       
    driver.find_element_by_xpath('/html/body/div[4]/table/tbody/tr/td[2]/a/span').click() 
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="shares"]').click() 
    driver.find_element_by_xpath('//*[@id="shares"]').clear() 
    driver.find_element_by_xpath('//*[@id="shares"]').send_keys('0.01')
    driver.find_element_by_xpath('/html/body/div[7]/div/div[1]/form/div[3]/div/button[3]').click() 

Then I can just add the 'Buy' (or whatever) variable in my If statement instead of that whole list of code.

if xxxxxxxxx
execute "Buy"
1
  • 2
    Have you tried using def? Commented Dec 17, 2020 at 23:27

2 Answers 2

1

You mean like a function?

def buy():
    driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[1]/div/div[1]/input').click()     
    driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN) 
    time.sleep(1)       
    driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/table/tbody/tr/td[2]/a/span').click() 
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="shares"]').click() 
    driver.find_element_by_xpath('//*[@id="shares"]').clear() 
    driver.find_element_by_xpath('//*[@id="shares"]').send_keys('0.01') 
    driver.find_element_by_xpath('/html/body/div[7]/div/div/div[1]/form/div[3]/div/button[3]').click() 

Now you can run your code just with buy().

if condition:
    buy()
Sign up to request clarification or add additional context in comments.

Comments

0

def x(): was the correct solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.