简体   繁体   English

页面刷新/重定向后找不到元素

[英]Unable to find element after page refresh/redirect

I am finding a hard time automating a basic UI test case. 我发现很难自动化基本的UI测试用例。 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. 我正在尝试为具有用户名和密码的用户登录模块的应用程序编写一些UI测试用例。 After login, there can be two scenarios as usual : Successful Login and Incorrect password. 登录后,通常会出现两种情况:成功登录和密码错误。

On Successful Login the Application URL changes. 成功登录后,应用程序URL会更改。 Eg www.abc.com/AA to www.abc.com/BB. 例如,从www.abc.com/AA到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. 现在的问题是,我能够打开页面,通过Selenium输入U / N和Pswd,但是当页面重定向发生/刷新时,单击“登录”按钮后我遇到了问题。

Webdriver is unable to find any element on the page. Webdriver无法在页面上找到任何元素。 I have already checked there is no IFrame on the page. 我已经检查过页面上没有IFrame。

I am using C# Selenium Web Driver in a .net Console application. 我在.net控制台应用程序中使用C#Selenium Web驱动程序。 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. 您可以尝试对条件URL进行条件循环检查,该URL等待X一段时间,直到它包含abc.com/BBB,然后在成功案例中检查Web元素的可见性,否则检查错误消息。 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 - 编辑:这是您的查询的片段(请等待,直到URL更改)我对C#样式并不熟悉,但是在Java中,您可以应用相同的逻辑-

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. 问题在于Selenium不知道页面已刷新,它正在“上一页”页面中寻找新控件。

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. 仅使用“发送键”并不能让Selenium判断发生了页面转换,您可以在文本区域中键入文本的段落。

If there is a login button then you should "click" it, and Selenium will then expect a page reload. 如果有一个登录按钮,则应“单击”它,然后Selenium将期望重新加载页面。 Otherwise you'll have to force Selenium to manually reload the page. 否则,您将不得不强制Selenium手动重新加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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