dHow to execute multiple lines of code, Python, Selenium

165 Views Asked by At

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"
2

There are 2 best solutions below

0
On

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

def x(): was the correct solution.