首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >硒中的StaleElementReferenceException

硒中的StaleElementReferenceException
EN

Stack Exchange QA用户
提问于 2015-06-12 05:54:58
回答 2查看 376关注 0票数 0

我被困在下面:

代码语言:javascript
复制
List<WebElement>lstType=driver.findElements(By.xpath("//select[@id='ddlGeneralType']/option"));

Thread.sleep(2000);
int i,j;
System.out.println("the no of type"+lstType.size());

for(i=1;i<=lstType.size();i++){
  lstType.get(i).click();
  List<WebElement> slctsubType=driver.findElements(By.xpath("//select[@id='ddlGeneralSubType']/option")); 

  System.out.println("the no of type"+slctsubType.size());
  for(j=1;j<=slctsubType.size();j++){
    try {
      slctsubType.get(j).click();
      Thread.sleep(5000);
      WebElement save= driver.findElement(By.xpath("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/a[text()='Save']"));
      save.click();
      Thread.sleep(5000);
      if(save.getText()==null){
        //  driver.findElement(By.xpath("//div[@id='generalClassificationModal']/div[@class='modal-footer']/button[text()='Close']")).click();
          driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
      }
      else {
        Thread.sleep(5000);
         //driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Close']"))).click();
        driver.findElement(By.xpath(("//div[@id='generalClassificationEditModal']/div[@class='modal-footer']/button[text()='Cancel']"))).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("//a[@href='#classification'and contains(@onclick,'OpenEditGeneralClassification();')]")).click();
      } 
    }
    catch(org.openqa.selenium.StaleElementReferenceException e){
      e.printStackTrace();
    }
  }
}

我收到一条由这一行代码引发的错误消息:

代码语言:javascript
复制
slctsubType.get(j).click(); 
EN

回答 2

Stack Exchange QA用户

发布于 2015-06-12 06:57:42

造成这种情况的原因有两个:

  1. 元素已被完全删除。
  2. 元素不再附加到DOM。

在您的例子中,我认为因为元素j在slcsubType中不可见,所以它不能单击slctsubType.get(j).click(); --我认为您应该使用Explicit Wait,等待这些元素出现,然后单击。

希望这能有所帮助。

票数 1
EN

Stack Exchange QA用户

发布于 2015-06-12 06:40:14

我认为您已经编写了从下拉列表中选择所有选项的代码。但您最多应该选择一个元素。这样你就可以得到“硒中的StaleElementReferenceException”。

这意味着对现有元素的引用被取消引用,这意味着element=null。

问题在for循环中:

代码语言:javascript
复制
for(j=1;j<=slctsubType.size();j++){
try{
    slctsubType.get(j).click();

在上面的代码中,您将从下拉列表中选择所有选项。在任何条件下,如值或文本,只能选择一个选项。

票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/13447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档