简体   繁体   中英

dHow to execute multiple lines of code, Python, Selenium

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"

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

def x(): was the correct solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM