简体   繁体   中英

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

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.

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(winodws[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM