Home > other >  How can I switch to the new open page after click button register? C# Selenium specflow
How can I switch to the new open page after click button register? C# Selenium specflow

Time:03-31

so I'm a newbie in C# selenium and I faced one problem(issue) I create several steps on one main page after I add information on the registration page I click to "register button" and this redirects me to the next new page, and my test cases failed. Because my webDriver is still on the previous page. But I should find a new element on a new page. How can I do it more simple way? Thank you in advance.

        [When(@"I add specific name")]
        public void WhenIAddSpecificName()
        {
            namelField.Click();
            namelField.Clear();
            Random randomName = new Random();
            int randomNumName = randomName.Next(0, 10000);
            namelField.SendKeys($"IK"   randomNumName);
            Thread.Sleep(3000);
            
        }

        [Then(@"I click registration button")]
        public void ThenIClickRegistrationButton()
        {
            registrationButton.Click();
            webDriver.SwitchTo().Window(webDriver.WindowHandles[1]);


        }

        [When(@"I add mobile telephone no")]
        public void WhenIAddMobileTelephoneNo()
        {            
            phoneField.Click();
            phoneField.SendKeys("2222");
            Thread.Sleep(5000);
            webDriver.Close();
        }

I want to find a new element on the new page.

CodePudding user response:

I would probably get all the handles before switching to it using driver.WindowHandles

So once you've performed a click then you should use the below code:

registrationButton.Click();
List<string> winodws = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(tabs[1]);
  • Related