Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我收到StaleElementReferenceException:元素未附加到页面文档_Java_Selenium_Selenium Webdriver_Staleelementreferenceexception - Fatal编程技术网

Java 我收到StaleElementReferenceException:元素未附加到页面文档

Java 我收到StaleElementReferenceException:元素未附加到页面文档,java,selenium,selenium-webdriver,staleelementreferenceexception,Java,Selenium,Selenium Webdriver,Staleelementreferenceexception,我收到StaleElementReferenceException:元素未附加到页面文档。我介绍了StackOverflow中已有的一些解决方案。它不起作用,继续抛出相同的错误。下面是我正在使用的抛出陈旧引用错误的代码 WebElement table2 = driver.findElement(By.cssSelector("body > div:nth-child(74) > div.sp-palette-container")); List<WebElement>

我收到
StaleElementReferenceException
:元素未附加到页面文档。我介绍了StackOverflow中已有的一些解决方案。它不起作用,继续抛出相同的错误。下面是我正在使用的抛出陈旧引用错误的代码

WebElement table2 = driver.findElement(By.cssSelector("body > div:nth-child(74) > div.sp-palette-container"));
List<WebElement> allrows2 = table2.findElements(By.tagName("div"));

    for(WebElement row2: allrows2){
        List<WebElement> cells = row2.findElements(By.tagName("span"));
        for(WebElement cell:cells){
                if (cell.getAttribute("title").equals("rgb(0, 158, 236)")) {
                cell.click(); 

                }          
          }
    } 
WebElement table2=driver.findElement(By.cssSelector(“body>div:nth child(74)>div.sp-palete-container”);
List allrows2=table2.findElements(按.tagName(“div”));
对于(WebElement第2行:所有第2行){
列表单元格=行2.findElements(按.tagName(“span”);
for(WebElement单元:单元){
if(cell.getAttribute(“title”).equals(“rgb(0158236)”){
单元格。单击();
}          
}
} 

因为单击找到的单元格会导致当前页面上的一些HTML更改,因此selenium会将页面(单击后)视为一个“新”页面(即使实际上没有重定向到另一个页面)

在循环的下一次迭代中,循环仍然引用属于“previous”页面的元素,这是“StateElementReference”异常的根本原因

因此,您需要在“新建”页面上再次找到这些元素,以更改来自“新建”页面的元素的引用

WebElement table2=driver.findElement(By.cssSelector(“body>div:nth child(74)>div.sp-palete-container”);
List allrows2=table2.findElements(按.tagName(“div”));
int rowSize,cellSize=0;
rowSize=allrows2.sie();

对于(int rowIndex=0;rowIndex,因为单击找到的单元格会导致当前页面上的一些HTML更改,因此selenium会将页面(单击后)视为“新”页面(即使实际上没有重定向到另一个页面)

在循环的下一次迭代中,循环仍然引用属于“previous”页面的元素,这是“StateElementReference”异常的根本原因

因此,您需要在“新建”页面上再次找到这些元素,以更改来自“新建”页面的元素的引用

WebElement table2=driver.findElement(By.cssSelector(“body>div:nth child(74)>div.sp-palete-container”);
List allrows2=table2.findElements(按.tagName(“div”));
int rowSize,cellSize=0;
rowSize=allrows2.sie();
对于(int rowIndex=0;rowIndex,如果您的用例是
在标题为rgb(0,158,236)的元素上单击()
您可以使用以下代码块:

    String baseURL = driver.getCurrentUrl();
    List<WebElement> total_cells = driver.findElements(By.xpath("//div[@class='sp-palette-container']//div//span"));
    int size = total_cells.size();
    for(int i=0;i<size;i++)
    {
        List<WebElement> cells = driver.findElements(By.xpath("//div[@class='sp-palette-container']//div//span"));
        if (cells.get(i).getAttribute("title").contains("rgb(0, 158, 236)")) 
        {
            cells.get(i).click(); 
            //do your other tasks
            driver.get(baseURL);
        }
    } 
String baseURL=driver.getCurrentUrl();
List total_cells=driver.findelelements(By.xpath(“//div[@class='sp-palete-container']//div//span”);
int size=total_cells.size();
对于(int i=0;i如果您的用例是
在标题为rgb(0,158,236)的元素上单击()
您可以使用以下代码块:

    String baseURL = driver.getCurrentUrl();
    List<WebElement> total_cells = driver.findElements(By.xpath("//div[@class='sp-palette-container']//div//span"));
    int size = total_cells.size();
    for(int i=0;i<size;i++)
    {
        List<WebElement> cells = driver.findElements(By.xpath("//div[@class='sp-palette-container']//div//span"));
        if (cells.get(i).getAttribute("title").contains("rgb(0, 158, 236)")) 
        {
            cells.get(i).click(); 
            //do your other tasks
            driver.get(baseURL);
        }
    } 
String baseURL=driver.getCurrentUrl();
List total_cells=driver.findelelements(By.xpath(“//div[@class='sp-palete-container']//div//span”);
int size=total_cells.size();
对于(int i=0;i在单击找到的元素后使用“break”。发生异常的原因是,在单击元素后,循环将继续

WebElement table2 = driver.findElement(By.cssSelector("body > div:nth-child(74) > div.sp-palette-container"));
List<WebElement> allrows2 = table2.findElements(By.tagName("div"));

    for(WebElement row2: allrows2){
        List<WebElement> cells = row2.findElements(By.tagName("span"));
        for(WebElement cell:cells){
                if (cell.getAttribute("title").equals("rgb(0, 158, 236)")) {
                cell.click(); 
                break;
                }          
          }
    } 
WebElement table2=driver.findElement(By.cssSelector(“body>div:nth child(74)>div.sp-palete-container”);
List allrows2=table2.findElements(按.tagName(“div”));
对于(WebElement第2行:所有第2行){
列表单元格=行2.findElements(按.tagName(“span”);
for(WebElement单元:单元){
if(cell.getAttribute(“title”).equals(“rgb(0158236)”){
单元格。单击();
打破
}          
}
} 
单击找到的元素后使用“中断”。出现异常是因为,单击元素后,循环将继续

WebElement table2 = driver.findElement(By.cssSelector("body > div:nth-child(74) > div.sp-palette-container"));
List<WebElement> allrows2 = table2.findElements(By.tagName("div"));

    for(WebElement row2: allrows2){
        List<WebElement> cells = row2.findElements(By.tagName("span"));
        for(WebElement cell:cells){
                if (cell.getAttribute("title").equals("rgb(0, 158, 236)")) {
                cell.click(); 
                break;
                }          
          }
    } 
WebElement table2=driver.findElement(By.cssSelector(“body>div:nth child(74)>div.sp-palete-container”);
List allrows2=table2.findElements(按.tagName(“div”));
对于(WebElement第2行:所有第2行){
列表单元格=行2.findElements(按.tagName(“span”);
for(WebElement单元:单元){
if(cell.getAttribute(“title”).equals(“rgb(0158236)”){
单元格。单击();
打破
}          
}
} 

添加相关的
HTML
以及您的目的。据我所知,您可以通过调整选择器将上述代码切换为2-3行代码。您不必重复2个列表。您可以使用适当的选择器创建1个列表,或者发布所附的HTML代码。感谢添加相关的
HTML
以及您的目的。如据我所知,通过调整选择器,您可以将上述代码切换为2-3行代码。您不必迭代2个列表。您可以使用适当的选择器创建1个列表,或者请附带HTML代码。谢谢