简体   繁体   中英

Unable to find element after page refresh/redirect

I am finding a hard time automating a basic UI test case. Using Selenium for the first time.

I am trying to write some UI test cases for an application which has User Login module with a username and password. After login, there can be two scenarios as usual : Successful Login and Incorrect password.

On Successful Login the Application URL changes. Eg www.abc.com/AA to www.abc.com/BB.

On Incorrect Password, page REFRESHES once and then show an error message on the same page.

Now the problem is , I am able to open the page, enter U/N and Pswd through Selenium but I face issue after clicking 'Login' button when page redirect happens/refreshes.

Webdriver is unable to find any element on the page. I have already checked there is no IFrame on the page.

I am using C# Selenium Web Driver in a .net Console application. Citing the code for refrnce:

       var options = new InternetExplorerOptions()
        {
            InitialBrowserUrl = URL,
            IntroduceInstabilityByIgnoringProtectedModeSettings = true,
        };
        var driver = new InternetExplorerDriver("www.abc.com/AA", options);
        driver.Navigate();

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        var username = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("userName"));
        });           
        username.SendKeys("User1");

        var password = driver.FindElement(By.Id("userPwd"));
        password.SendKeys("Password@123");

// Login button pressed in the next line          
        password.SendKeys(Keys.Enter);

//WAIT FOR RESULT
  WebDriverWait waiter = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

// This below line Shows error "Unable to find Element..."
        IWebElement categoryList = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("SAMPLE-DROPDOWN-ID"));
        });           

        categoryList.SendKeys("1");

You can try having a conditional loop with a check on the URL that wait for an X period of time till it contains abc.com/BBB then check the visibility of the web element on Successful case, else check the error message. This will also easier for you to debug the issue and make your test less prone to errors.

EDIT: Here's the snippet to your query (Wait until URL changes) I am not well versed with the C# style, but here it is in Java, you can apply the same logic -

public static String start_URL = "http://example.com/";

public static WebDriver driver = null;
public static WebDriverWait wait = new WebDriverWait(driver, 10);
public static String prev_URL = null;
public static String curr_URL = null;

public static void main(String[] args) {

    driver = new FirefoxDriver();

    driver.navigate().to(start_URL);

    prev_URL = driver.getCurrentUrl();

    //do login operation - enter user name and password

    ExpectedCondition e = new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driverNew) {
            return (driverNew.getCurrentUrl() != prev_URL);
        }
    };

    wait.until(e);
    curr_URL = driver.getCurrentUrl();
    System.out.println(curr_URL);

}

The problem is that Selenium doesn't know that the page has refreshed, and it is looking for the new control in the "previous" page.

Just using "send keys" doesn't allow Selenium to figure out that a page transition has occurred, you could just be typing paragraphs of text into a text area.

If there is a login button then you should "click" it, and Selenium will then expect a page reload. Otherwise you'll have to force Selenium to manually reload the page.

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