简体   繁体   中英

why the modal window doesn't show up when clicked by selenium

I try to click on the login button(Đăng nhập) to show up the login box, but fail to achieve it.

The loginbox just doesn't show up.

Selenium, webdriver are all latest version

    using (IWebDriver driver = new ChromeDriver())
            {

                driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
         //       driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

              //  driver.FindElement(By.XPath("//a[@href='#loginform']//span")).Click();
            //    driver.FindElement(By.XPath("//a[@href='#loginform']")).Click();

            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));               
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[@href='#loginform']"))).Click();
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[@href='#loginform']//span"))).Click();

                wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_username"))); 
                wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_password"))); 

          //       var loginBox= wait.Until(ElementIsClickable(By.Id("loginform"))); >> fail
                driver.Scripts().ExecuteScript("document.getElementById('navbar_username').style.display='inline';");
                driver.Scripts().ExecuteScript("document.getElementById('navbar_password').style.display='inline';");


                Console.ReadKey();
            }

C# extension:

 public static IJavaScriptExecutor Scripts(this IWebDriver driver)
    {
        return (IJavaScriptExecutor)driver;
    }

There are 2 problems.

1- There's a webpage coming up prior to the actual forum page, when you are navigating to the site. Below is the image for that:

在此处输入图片说明

So you have to click on the button, that is highlighted above first. And, then after you will be able to navigate to the forum's page.

2- Your button is certainly getting clicked, but since the webpage has not properly loaded, the click action is not proceeding.

Hence, you need to wait for certain element that gets loaded when the page is loaded properly.

Below code will help you out:-

using (IWebDriver driver = new ChromeDriver())
            {

                driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");

                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); //Give the implicit wait time

               driver.FindElement(By.XPath("//button[@id='btnSubmit1']")).Click();// Clicking on the button present in prior page of forum

              //Waiting till the element that marks the page is loaded properly, is visible
              var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));  

              wait.Until(ExpectedConditions.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='vtlai_topx']/a")));

               driver.FindElement(By.XPath("//a[@href='#loginform']")).Click();

...

You can proceed with rest then.

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