We present visible to near-infrared spectra of highly charged Yb (the atomic number 70) and W (74) obtained with a compact electron beam ion trap. By observing the dependence on the electron beam energy, the charge state that should be assigned to the observed lines is determined. For In-like Yb21+ and W25+ and Sn-like W24+, the experimental spectra are compared with theoretical calculations done by the CI+all-order method. In each spectrum, the most prominent line is identified as the magnetic dipole transition from the first excited fine structure level to the ground level in the ground electronic configuration, 4f(3) for In-like ions and 4f(4) for Sn-like ions.