簡體   English   中英

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

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

我發現很難自動化基本的UI測試用例。 第一次使用硒。

我正在嘗試為具有用戶名和密碼的用戶登錄模塊的應用程序編寫一些UI測試用例。 登錄后,通常會出現兩種情況:成功登錄和密碼錯誤。

成功登錄后,應用程序URL會更改。 例如,從www.abc.com/AA到www.abc.com/BB。

在“密碼錯誤”上,刷新一次頁面,然后在同一頁面上顯示錯誤消息。

現在的問題是,我能夠打開頁面,通過Selenium輸入U / N和Pswd,但是當頁面重定向發生/刷新時,單擊“登錄”按鈕后我遇到了問題。

Webdriver無法在頁面上找到任何元素。 我已經檢查過頁面上沒有IFrame。

我在.net控制台應用程序中使用C#Selenium Web驅動程序。 引用引用代碼:

       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");

您可以嘗試對條件URL進行條件循環檢查,該URL等待X一段時間,直到它包含abc.com/BBB,然后在成功案例中檢查Web元素的可見性,否則檢查錯誤消息。 這也將使您更容易調試問題並減少測試出錯的可能性。

編輯:這是您的查詢的片段(請等待,直到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);

}

問題在於Selenium不知道頁面已刷新,它正在“上一頁”頁面中尋找新控件。

僅使用“發送鍵”並不能讓Selenium判斷發生了頁面轉換,您可以在文本區域中鍵入文本的段落。

如果有一個登錄按鈕,則應“單擊”它,然后Selenium將期望重新加載頁面。 否則,您將不得不強制Selenium手動重新加載頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM