Streptococcosis disease caused by Streptococcus agalactiae (Group B Streptococcus, GBS) results in a huge economic loss of tilapia culture. It is urgent to find new antimicrobial agents against streptococcosis. In this study, 20 medicinal plants were evaluated in vitro and in vivo to obtain medicinal plants and potential bioactive compounds against GBS infection. The results showed that the ethanol extracts of 20 medicinal plants had low or no antibacterial properties in vitro, with a minimal inhibitory concentration ≥256 mg/L. Interestingly, in vivo tests showed that 7 medicinal plants could significantly inhibit GBS infection in tilapia, and Sophora flavescens (SF) had the strongest anti‐GBS activity in tilapia, reaching 92.68%. SF could significantly reduce the bacterial loads of GBS in different tissues (liver, spleen and brain) of tilapia after treated with different tested concentrations (12.5, 25.0, 50.0 and 100.0 mg/kg) for 24 h. Moreover, 50 mg/kg SF could significantly improve the survival rate of GBS‐infected tilapia by inhibiting GBS replication. Furthermore, the expression of antioxidant gene cat, immune‐related gene c‐type lysozyme and anti‐inflammatory cytokine il‐10 in liver tissue of GBS‐infected tilapia significantly increased after treated with SF for 24 h. Meanwhile, SF significantly reduced the expression of immune‐related gene myd88 and pro‐inflammatory cytokines il‐8 and il‐1β in liver tissue of GBS‐infected tilapia. The negative and positive models of UPLC‐QE‐MS, respectively, identified 27 and 57 components of SF. The major components of SF extract in the negative model were α, α‐trehalose, DL‐malic acid, D‐ (−)‐fructose and xanthohumol, while in the positive model were oxymatrine, formononetin, (−)‐maackiain and xanthohumol. Interestingly, oxymatrine and xanthohumol could significantly inhibit GBS infection in tilapia. Taken together, these results suggest that SF can inhibit GBS infection in tilapia, and it has potential for the development of anti‐GBS agents.