Java 如何处理StaleElementReferenceException
我有一个场景,在这个场景中,我试图循环一个条形图上的许多元素,直到找到“rect”标记名。当我点击“rect”标记名时,从图表中选择单个条,并重定向到另一个页面。请参见下面我正在使用的条形图的图像: 作为参考,我正在使用的条形图位于右上方。我要执行的测试是单击图表中的第一个条形图;这样做会将我重定向到适当的页面。为了做到这一点,我使用Eclipse(Java)在SeleniumWebDriver中编写了以下代码:Java 如何处理StaleElementReferenceException,java,eclipse,selenium,webdriver,Java,Eclipse,Selenium,Webdriver,我有一个场景,在这个场景中,我试图循环一个条形图上的许多元素,直到找到“rect”标记名。当我点击“rect”标记名时,从图表中选择单个条,并重定向到另一个页面。请参见下面我正在使用的条形图的图像: 作为参考,我正在使用的条形图位于右上方。我要执行的测试是单击图表中的第一个条形图;这样做会将我重定向到适当的页面。为了做到这一点,我使用Eclipse(Java)在SeleniumWebDriver中编写了以下代码: WebElement deliveredChartDailyFocus=driv
WebElement deliveredChartDailyFocus=driver.findElement(By.id(“每日交付图表”);
deliveredChartDailyFocus.click();
列表子项=deliveredChartDailyFocus.findElements(按.tagName(“rect”));
迭代器iter=children.Iterator();
while(iter.hasNext()){
WebElement we=iter.next();
if(we.isDisplayed()){
we.click();
}
上面的代码点击了“rect”元素并将我重定向到相应的页面,因此一切看起来都很好。但是,当我点击页面时,我得到了一个错误,因为代码仍然在寻找新页面上没有的“rect”值
您会注意到上面缺少了一行“break”……这是因为,在调试代码时,我发现在循环中迭代时,click事件直到第三次迭代才开始,我假设是因为“rect”元素不可见?因此,如果我输入“break”语句它在第一次迭代后退出循环,因此我永远不会到达执行“单击”事件以导航到新页面的部分
本质上,我所追求的是一种能够根据需要多次循环的方法,直到找到合适的“rect”元素。单击该元素后,我被重定向到新页面……只有在该点上,我才希望循环退出,以便不显示“NoTouchElementException错误”
如果需要更多详细信息,请告诉我,非常感谢您的指导。Andy,这里的问题是
DOM
刷新。您不能简单地获取IWebElements的集合,然后来回迭代和单击。您可以找到元素的计数,并且每次访问e转到页面,查找要动态单击的元素。有关实现,请参阅
public void ClickThroughLinks()
{
_driver.Navigate().GoToUrl("http://www.cnn.com/");
//Maximize the window so that the list can be gathered successfully.
_driver.Manage().Window.Maximize();
//find the list
By xPath = By.XPath("//h2[.='The Latest']/../li//a");
var linkCollection = _driver.FindElements(xPath);
for (int i = 0; i < linkCollection.Count; i++)
{
//wait for the elements to be exist
new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(xPath));
//Click on the elements by index
if (i<=3)
{
_driver.FindElements(xPath)[i].Click();
}
else
{
break;
}
_driver.Navigate().Back();
_driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
}
}
public void ClickThroughLinks()
{
_driver.Navigate().gotour(“http://www.cnn.com/");
//最大化窗口,以便成功收集列表。
_driver.Manage().Window.Maximize();
//查找列表
By xPath=By.xPath(“//h2[.='Latest']/../li//a”);
var linkCollection=_driver.FindElements(xPath);
for(int i=0;i 如果(iAndy,这里的问题是DOM
refresh。您不能简单地获取IWebElements
的集合,然后反复遍历并来回单击。您可以找到元素的计数,并且每次来到页面时都可以找到要动态单击的元素。有关实现,请参阅
public void ClickThroughLinks()
{
_driver.Navigate().GoToUrl("http://www.cnn.com/");
//Maximize the window so that the list can be gathered successfully.
_driver.Manage().Window.Maximize();
//find the list
By xPath = By.XPath("//h2[.='The Latest']/../li//a");
var linkCollection = _driver.FindElements(xPath);
for (int i = 0; i < linkCollection.Count; i++)
{
//wait for the elements to be exist
new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(xPath));
//Click on the elements by index
if (i<=3)
{
_driver.FindElements(xPath)[i].Click();
}
else
{
break;
}
_driver.Navigate().Back();
_driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
}
}
public void ClickThroughLinks()
{
_driver.Navigate().gotour(“http://www.cnn.com/");
//最大化窗口,以便成功收集列表。
_driver.Manage().Window.Maximize();
//查找列表
By xPath=By.xPath(“//h2[.='Latest']/../li//a”);
var linkCollection=_driver.FindElements(xPath);
for(int i=0;i 如果(iAndy,这里的问题是DOM
refresh。您不能简单地获取IWebElements
的集合,然后反复遍历并来回单击。您可以找到元素的计数,并且每次来到页面时都可以找到要动态单击的元素。有关实现,请参阅
public void ClickThroughLinks()
{
_driver.Navigate().GoToUrl("http://www.cnn.com/");
//Maximize the window so that the list can be gathered successfully.
_driver.Manage().Window.Maximize();
//find the list
By xPath = By.XPath("//h2[.='The Latest']/../li//a");
var linkCollection = _driver.FindElements(xPath);
for (int i = 0; i < linkCollection.Count; i++)
{
//wait for the elements to be exist
new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(xPath));
//Click on the elements by index
if (i<=3)
{
_driver.FindElements(xPath)[i].Click();
}
else
{
break;
}
_driver.Navigate().Back();
_driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
}
}
public void ClickThroughLinks()
{
_driver.Navigate().gotour(“http://www.cnn.com/");
//最大化窗口,以便成功收集列表。
_driver.Manage().Window.Maximize();
//查找列表
By xPath=By.xPath(“//h2[.='Latest']/../li//a”);
var linkCollection=_driver.FindElements(xPath);
for(int i=0;i 如果(iAndy,这里的问题是DOM
refresh。您不能简单地获取IWebElements
的集合,然后反复遍历并来回单击。您可以找到元素的计数,并且每次来到页面时都可以找到要动态单击的元素。有关实现,请参阅
public void ClickThroughLinks()
{
_driver.Navigate().GoToUrl("http://www.cnn.com/");
//Maximize the window so that the list can be gathered successfully.
_driver.Manage().Window.Maximize();
//find the list
By xPath = By.XPath("//h2[.='The Latest']/../li//a");
var linkCollection = _driver.FindElements(xPath);
for (int i = 0; i < linkCollection.Count; i++)
{
//wait for the elements to be exist
new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(xPath));
//Click on the elements by index
if (i<=3)
{
_driver.FindElements(xPath)[i].Click();
}
else
{
break;
}
_driver.Navigate().Back();
_driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
}
}
public void ClickThroughLinks()
{
_driver.Navigate().gotour(“http://www.cnn.com/");
//最大化窗口,以便成功收集列表。
_driver.Manage().Window.Maximize();
//查找列表
By xPath=By.xPath(“//h2[.='Latest']/../li//a”);
var linkCollection=_driver.FindElements(xPath);
for(int i=0;i 如果(i一旦你进入新页面,所有这些rect