Gravitational-wave (GW) detectors can contribute to the measurement of cosmological parameters and to testing the dark-energy sector of alternatives to \LambdaCDM, by using standard sirens. In this paper we focus on binary neutron stars with a counterpart detected through a gamma-ray burst (GRB), both at a second-generation network made by advanced LIGO+advanced Virgo+LIGO India+Kagra, and at third-generation (3G) detectors, discussing in particular the cases of a single Einstein Telescope (ET), and of a network of ET plus two Cosmic Explorer (CE). We construct mock catalogs of standard sirens, using different scenarios for the local merger rate and for the detection of the electromagnetic counterpart. For 3G detectors we estimate the coincidences with a GRB detector with the characteristics of the proposed THESEUS mission. We discuss how these standard sirens with a GRB counterpart can improve the determination of cosmological parameters (and particularly of H_0) in \LambdaCDM, and we then study how to extract information on dark energy, considering both a non-trivial dark energy equation of state and modified GW propagation. We find that a 2G detector network can already reach, over several years of data taking, an interesting sensitivity to modified GW propagation, while a single ET detector would have a remarkable potential for discovery. We also find that, to fully exploit the potential of a ET+CE+CE network, it is necessary a much stronger program of search for electromagnetic counterparts (or else to resort to statistical methods for standard sirens), and furthermore gravitational lensing can become a limiting factor.