Home > Blockchain >  Selenium C# same id element
Selenium C# same id element

Time:11-14

So I've been stuck with this for some time now. I have an element id=nextButton. I need to be able to click it several times. The process looks like this:

Page1 -> nextButton.click() -> Page2 -> nextButton.click() -> ...

my not working solution for this was:

Page1

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
IWebElement nextButton = driver.FindElement(By.XPath("//*[@id=\"nextButton\"]"));
nextButton.Click();

Page2

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
IWebElement nextButton2 = driver.FindElement(By.XPath("//*[@id=\"nextButton\"]"));
nextButton2.Click();

Its no working.. It loads the Page1 than clicks the nextButton than new page shows up and it stops..
Please help :)

CodePudding user response:

I guess you need to put some delay after clicking the nextButton to make the first page starting loading the second page, then to wait for the nextButton to be clickable on the second page and only then to click it.
What happens in your flow is: you trying to click the nextButton immediately after the previous click, while you still on the first page.
This code should work for you:

using WaitHelpers = SeleniumExtras.WaitHelpers;

wait.Until(WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='nextButton']")).Click();
Thread.Sleep(500)
wait.Until(WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='nextButton']")).Click();

CodePudding user response:

Thanks @Prophet. I just put Thread.Sleep(500) between my code so it looks like this now:

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); IWebElement nextButton = driver.FindElement(By.XPath("//*[@id="nextButton"]")); nextButton.Click();

Thread.Sleep(500);

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); IWebElement nextButton2 = driver.FindElement(By.XPath("//*[@id="nextButton"]")); nextButton2.Click();

And it is working :)

  • Related