As I have been a frequent traveller on this train corridor for several years now, a few comments:
Don't depend on same day purchase. Soft seats are likely to have sold out and even a 3-3 seat in 2nd is not guaranteed. I.e your anticipated 11am (just as an example) departure from Shanghai might turn into 3-4 pm or later due to lack of seats.
Buying black market tickets is possible but scam artists are eagerly waiting for you...
You can buy both ways in Shanghai depending on what day your planned return is.
Ask your hotel to do ticket purchases in advance and pay the difference. Or if you are in Shanghai a day or two before the planned trip then visit a ticket kiosk as
moondog mentioned. One easy to find is downstairs at the Maglev city end terminus.