单击Angular WebPage中的TableRow时,StaleElementException [英] StaleElementException when Clicking on a TableRow in an Angular WebPage

查看:78
本文介绍了单击Angular WebPage中的TableRow时,StaleElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div id="crm" class="row gridrow clickable ng-scope" ng-repeat="customer in customerList" ng-click="gotoRecord(customer.id)">
     <i class="col m1 s1 tiny fa fa-male"></i>
     <div class="col m3 s11 ng-binding"> Allard</div>
     <div class="col m2 s12 ng-binding"></div>
</div>

我有此HTML代码段,它是对nameCustomer'Allard'的Customer进行搜索操作后显示的一行.我想单击此客户以继续下一页,但是在大多数情况下,这会导致StaleElementException.

I have this snippet of HTML, it's one row displaying as a result of a search action for a Customer with nameCustomer 'Allard'. I want to click on this customer to continue to the next page but most of the time this results in a StaleElementException.

我尝试了两种不同的方法,使用量角器和不使用量角器.

I tried it two different ways, using Protractor and without Protractor.

第一种方式:

IWebElement elem = driver.FindElement(By.XPath("//*[contains(text(),'" + nameCustomer + "')]//parent::div[contains(@id,'crm')]"));
        ExplicitWait.WaitAndClick(driver, elem);

第二种方式:

var customers = driver.FindElements(NgBy.Repeater("customer in customerList"));
        foreach (var customer in customers)
        {
            if (elem.Text.Equals(nameCustomer))
            {
                elem.Click();
            }
        }

推荐答案

问题(我认为)

使用StaleReferenceExceptions,以前创建的IWebElement不再附加到DOM(您可能已经知道这一点).最有可能发生的事情是这样的:
1:您单击搜索.
2:Selenium执行driver.FindElement(...)并找到匹配的元素.
3:然后搜索功能完成,并且DOM更新.以前找到的旧IWebElement不见了.
4:然后 Selenium尝试单击该元素(该元素不再存在,导致StaleElementException.有一个元素 matches 匹配以前的元素,但不是)在硒的眼中是相同的元素.)

The problem (I think)

With StaleReferenceExceptions, an IWebElement that was previously created is no longer attached to the DOM (you probably already know this). What's most likely happening is this:
1: You click search.
2: Selenium executes driver.FindElement(...) and finds a matching element.
3: Then the search function finishes and the DOM updates. The old IWebElement found previously is gone.
4: Then Selenium tries to click on the element (which no longer exists, causing the StaleElementException. There is an element that matches the one that was there before, but it's not the same element in Selenium's eyes.)

您关于这种情况的声明大多数时候" 使我怀疑情况更是如此,因为异常将取决于事件的顺序,而事件的顺序将取决于相对的事件硒与网页的速度.

Your statement that this happens "most of the time" makes me suspect this is the case even more, because the exception would depend on the order of events, which would vary depending on the relative speeds of Selenium vs. the web-page.

您需要在页面上找到一些内容,这些内容将指示Selenium搜索操作已完成.这就是编写GUI自动化脚本的创造力所在.如果页面上您知道某些内容会因加载而发生变化,请进行明确的等待以确保完成.也许会出现一个加载栏,或者搜索完成后会显示一条消息.在单击搜索之前,您可以获取一个与您的搜索不匹配的元素,然后进行明确的等待以确保其消失,然后再继续查找您的执行希望在那里.

You need to find something on the page that will indicate to Selenium that the search action is done. This is where the creativity of writing GUI automation scripts comes in. If there is something on the page that you know will change as a result of the load, craft an explicit wait to ensure that is complete. Maybe there is a loading bar that shows up, or a message that appears when the search is done. You could grab an element that doesn't match your search before clicking search, then do an explicit wait to make sure it disappears before going on to look for the result you do expect to be there.

下面看起来像这样.

# Search action performed before this.
WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(secondsToWait));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath( expressionForElementThatWillDissapear )));


IWebElement elem = driver.FindElement(By.XPath("//*[contains(text(),'" + nameCustomer + "')]//parent::div[contains(@id,'crm')]"));
            ExplicitWait.WaitAndClick(driver, elem);

我建议使该方法实现上面的显式等待.这些情况会经常出现.

I'd recommend making the method to implement the explicit wait above. These situations will come up often.

这篇关于单击Angular WebPage中的TableRow时,StaleElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆