Helicobacter pylori infection is an important causal factor of gastric cancer and peptic ulcer disease and is associated with immune thrombocytopenic purpura and functional dyspepsia. In H pylori strains, point mutations in the 23S rRNA and gyrA genes are associated with clarithromycin resistance and levofloxacin resistance, respectively. Whether the efficacy of molecular testing-guided therapy is non-inferior to that of susceptibility testing-guided therapy for H pylori eradication is unclear. Therefore, we aimed to compare the efficacy and safety of molecular testing-guided therapy and traditional culture-based susceptibility testing-guided therapy in first-line and third-line treatment of H pylori infection. We did two multicentre, open-label randomised trials in Taiwan. In trial 1 (done at seven hospitals), treatment-naive individuals infected with H pylori who were aged 20 years or older were eligible for study inclusion. In trial 2 (done at six hospitals), individuals aged 20 years or older who failed treatment after two or more eradication therapies for H pylori infection were eligible for enrolment. Eligible patients were randomly assigned (1:1) to receive either molecular testing-guided therapy or susceptibility testing-guided therapy. The randomisation sequence was generated by computer using permuted block randomisation with a block size of 4. All investigators were masked to the randomisation sequence. Clarithromycin and levofloxacin resistance were determined by agar dilution test for measuring minimum inhibitory concentrations in the susceptibility testing-guided therapy group, and by PCR and direct sequencing for detection of 23S rRNA and gyrA mutations in the molecular testing-guided therapy group. Study participants received clarithromycin sequential therapy, levofloxacin sequential therapy, or bismuth quadruple therapy according to the resistance status to clarithromycin and levofloxacin. The 13C-urease breath test was used to determine the status of H pylori infection at least 6 weeks after eradication therapy. The primary outcome was the eradication rate by intention-to-treat analysis. The frequency of adverse effects was analysed in patients with available data. The prespecified margins for non-inferiority were 5% for trial 1 and 10% for trial 2. The trials are ongoing for post-eradication follow-up and registered with ClinicalTrials.gov, NCT03556254 for trial 1, and NCT03555526 for trial 2. Between March 28, 2018, and April 23, 2021, 560 eligible treatment-naive patients with H pylori infection were recruited and randomly assigned to the molecular testing-guided therapy group or the susceptibility testing-guided therapy group in trial 1. Between Dec 28, 2017, and Oct 27, 2020, 320 eligible patients with refractory H pylori infection were recruited and randomly assigned to the molecular testing-guided therapy group or the susceptibility testing-guided therapy group in trial 2. 272 men and 288 women were recruited for trial 1, and 98 men and 222 women were recruited for trial 2. In first-line H pylori treatment, infection was eradicated in 241 (86%, 95% CI 82-90) of 280 patients in the molecular testing-guided therapy group and 243 (87%, 83-91) of 280 patients in the susceptibility testing-guided therapy group by intention-to-treat analysis (p=0·81). In third-line H pylori treatment, infection was eradicated in 141 (88%, 83-93) of 160 patients in the molecular testing-guided therapy group and 139 (87%, 82-92) of 160 patients in the susceptibility testing-guided therapy group by intention-to-treat analysis (p=0·74). The difference in the eradication rate between the molecular testing-guided therapy group and the susceptibility testing-guided therapy group was -0·7% (95% CI -6·4 to 5·0; non-inferiority p=0·071) in trial 1 and 1·3% (-6·0 to 8·5; non-inferiority p=0·0018 in trial 2 by intention-to-treat analysis. We found no difference in adverse effects across both treatment groups in trial 1 and trial 2. Molecular testing-guided therapy was similar to susceptibility testing-guided therapy in first-line therapy and non-inferior to susceptibility testing guided therapy in third-line treatment of H pylori infection, supporting the use of molecular testing-guided therapy for H pylori eradication. Ministry of Science and Technology of Taiwan, and Centre of Precision Medicine of the Higher Education Sprout Project by the Ministry of Education of Taiwan.