Shelley earned a law degree from the University of San Francisco in 1932. He served in the United States Coast Guard during World War II and was a member of the California State Senate from 1939 to 1947. He ran an unsuccessful race for the Lieutenant Governor's office against Goodwin Knight in 1946. Shelley would then make his mark as a leader of the California delegation to the 1948 Democratic National Convention, when he helped marshal his state's votes to support a strong civil rights plank. Shelley entered the United States House of Representatives in 1949 and served until 1964, when he stepped down to be inaugurated Mayor of San Francisco after winning the November, 1963 election by nearly a 12-point margin against his nearest opponent, Harold Dobbs (50-38.5%).