Java,Selenium:按钮单击WebElement后重新加载页面时出现陈旧元素异常
在我正在自动化的网页中,我希望根据同一行中下一个单元格的文本值,选择表中单元格包含的Java,Selenium:按钮单击WebElement后重新加载页面时出现陈旧元素异常,java,selenium,selenium-webdriver,automated-tests,Java,Selenium,Selenium Webdriver,Automated Tests,在我正在自动化的网页中,我希望根据同一行中下一个单元格的文本值,选择表中单元格包含的复选框 但是,每次单击复选框,页面都会重新加载,因此在选中第一个复选框后,我会得到一个StaleElementException ArrayList<WebElement> rows = (ArrayList<WebElement>)driver.findElements(By.xpath("html/body/form/table/tbody/tr[contains(@class, 'c
复选框
但是,每次单击复选框,页面都会重新加载,因此在选中第一个复选框后,我会得到一个StaleElementException
ArrayList<WebElement> rows = (ArrayList<WebElement>)driver.findElements(By.xpath("html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr"));
for (WebElement row: rows) {
if(row.getText().contains("Accept"))
{
ArrayList<WebElement> cells =(ArrayList<WebElement>)row.findElements(By.xpath("td"));
int i=1;
for (WebElement cell: cells)
{
if(i==11)//the 12 the cell contains the value "Accept"
{
cell.click(); //wait.until(ExpectedConditions.presenceOfElementLocated(By.name("xmlCustomizationInput")));
//rows = (ArrayList<WebElement>)driver.findElements(By.xpath("html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr"));
}
i++;
}
}
ArrayList rows=(ArrayList)driver.findElements(By.xpath(“html/body/form/table/tbody/tr[contains(@class,'configsubcell_plain'))]/td[contains(@class,'configsubcell_plain')]/table/tbody/tr”);
对于(WebElement行:行){
if(row.getText()包含(“接受”))
{
ArrayList cells=(ArrayList)row.findElements(By.xpath(“td”);
int i=1;
for(WebElement单元:单元)
{
如果(i==11)//12,则单元格包含值“Accept”
{
cell.click();//wait.until(ExpectedConditions.presenceOfElementLocated(By.name(“xmlCustomizationInput”));
//rows=(ArrayList)driver.findElements(By.xpath(“html/body/form/table/tbody/tr[contains(@class,'configsubcell_plain'))]/td[contains(@class,'configsubcell_plain')]/table/tbody/tr”);
}
i++;
}
}
该网页设计有许多嵌套表,因此我必须使用两个数组列表来标识我的产品
单击之后,我尝试使用等待
条件并重新初始化行,但这也导致了相同的StaleElementException
请帮助我检查所有复选框,其值为Accept,即使在每次单击后重新加载页面。我已通过迭代整个页面找到包含“Accept”文本的行并将位置存储在数组中,解决了此问题,如下所示
int i=1,j=0,rownum=0;
if(row.getText().contains("Accept"))
{
ArrayList<WebElement> cells =(ArrayList<WebElement>)row.findElements(By.xpath("td"));
for (WebElement cell: cells)
{
if(i==11)//the 12 the cell contains the value "Accept"
{
row_cell[j]=rownum;
j++;
}
}
}
inti=1,j=0,rownum=0;
if(row.getText()包含(“接受”))
{
ArrayList cells=(ArrayList)row.findElements(By.xpath(“td”);
for(WebElement单元:单元)
{
如果(i==11)//12,则单元格包含值“Accept”
{
row_cell[j]=rownum;
j++;
}
}
}
然后我直接迭代给出xpath并存储了行号,单元格号总是11,所以我硬编码了值。为了让循环等待,直到复选框被选中,我将它保留在一个while循环中。在检查之后,页面将被重新加载,再次出现异常,我让驱动程序等待并重新分配webelement和g继续
for(int l=0; l<row_cell.length; l++) {
if(row_cell[l]!=0)
{
apath="html/body/form/table/tbody/tr[contains(@class, 'configsubcell_plain')]/td[contains(@class, 'configsubcell_plain')]/table/tbody/tr["+row_cell[l]+"]/td[11]";
System.out.println("path is: "+apath);
WebElement rows1;
try
{
rows1 = driver.findElement(By.xpath(apath));`rows1.click();
while(!rows1.isSelected())`{
System.out.println("in while");`}
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
System.out.println("inside catch");
WebDriverWait wait1 = new WebDriverWait(driver, 6);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.name("xmlCustut")));
rows1 = driver.findElement(By.xpath(apath));
continue;
} `
对于(int l=0;lca)您是否可以提供该网页以及html文档?该网页是一个内部网站。谢谢