Using Selenium how to select two different elements that have the same ID?

Asked

Viewed 1,053 times

0

My attempt with xPath or by ID was unsuccessful.

var options = new ChromeOptions();
        options.AddArgument("--disable-gpu");

        var chromeDriver = new ChromeDriver(options);

        chromeDriver.Navigate().GoToUrl("http://radar.tntbrasil.com.br/radar/public/login");

        chromeDriver.FindElementById("login").SendKeys("");
        chromeDriver.FindElementById("senha").SendKeys("");

        chromeDriver.FindElement(By.XPath("//*[@id=\"login\"]/a")).Click();
        chromeDriver.FindElementById("login").SendKeys(Keys.Enter);

        chromeDriver.Close();

Follow the images to facilitate understanding:

Imagem do site onde mostra que a caixa de texto para informar o login e o botão acessar possuem o mesmo id

Código fonte onde busco o botão Login

  • Good.. First the two elements should not have the same ID... but you can try to differentiate by type

  • Can you tell me how to do that? Why I forgot to tell you but my attempt at xPath was unsuccessful.

1 answer

1


Since the elements possess the same id, although this is wrong, instead of your XPath query for all elements that have id=login, //*[@id="login"], you can identify which type of element with this id you are looking for.

//Para o input do login
chromeDriver.FindElement(By.Xpath("//input[@id=\"login\"]")).SendKeys("");

//Para o click no botão
chromeDriver.FindeElement(By.XPath("//a[@id=\"login\"]")).Click();
  • Thank you very much, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.