Java 如何将上一页中的值与webdriver中新打开页中的值进行比较
我试图将页面中显示的交易编号与用户单击“交易编号”链接后下一页中显示的交易编号进行比较 我尝试了以下方法:Java 如何将上一页中的值与webdriver中新打开页中的值进行比较,java,selenium,Java,Selenium,我试图将页面中显示的交易编号与用户单击“交易编号”链接后下一页中显示的交易编号进行比较 我尝试了以下方法: // -----------------------Selecting values in the form------------------- // Call the function to select a date from calendar e.g. 2016/04/19 ClaimsOnline_fields.pickExpDate("19",
// -----------------------Selecting values in the form-------------------
// Call the function to select a date from calendar e.g. 2016/04/19
ClaimsOnline_fields.pickExpDate("19",4,2016);
// In the Transaction Types dropdown, select a value e.g. "Realtime Transactions Only"
TransTypesDrop = new Select(drivers.findElement(By.xpath(".//*[@id='type']")));
TransTypesDrop.selectByVisibleText("Realtime Transactions Only");
// In the Dataset dropdown, select a value e.g. "5 - DJCJ0005"
DatasetDrop = new Select(Utilities.drivers.findElement(By.xpath(".//*[@id='logbook_form']/table/tbody/tr[1]/td/fieldset/table/tbody/tr[7]/td[2]/select")));
DatasetDrop.selectByVisibleText("5 - DJCJ0005");
// Click the Show button
drivers.findElement(By.xpath(".//*[@id='logbook_form']/table/tbody/tr[2]/td/input[1]")).click();
Utilities.wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/table[2]")));
batchVal = drivers.findElements(By.className("results"));
//----------------------- Drill into test result --------------------------
List<String> all_elements_text=new ArrayList<>();
// Click the first result
for(int i=0; i<batchVal.size(); i++ ){
// Click the Transaction No of the first result
batchVal.get(1).click();
// The next page is opened
Utilities.wait.until(ExpectedConditions.presenceOfElementLocated(By.className("jqi")));
Utilities.wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("jqi")));
drivers.findElement(By.className("jqidefaultbutton")).click();
Utilities.wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/p")));
String TransNo = drivers.findElement(By.xpath("html/body/p")).getText();
Utilities.wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/p")));
// Add the value from the search result into the array
all_elements_text.add(batchVal.get(i).getText());
// get the currently displayed Transaction Num
String TransNoTrimmed = TransNo.substring(34,48);
//
if(all_elements_text.get(1).equals(TransNoTrimmed)){
System.out.println("Test Passed - Claims Online - Logbook - Drill into Results - Transaction Number matches one displayed in the drilled results i.e. "+TransNoTrimmed);
}
break;
}
当我运行此命令时,会出现以下错误:
主线程中的异常
org.openqa.selenium.StaleElementReferenceException:stale元素
引用:元素未附加到页面文档
会话信息:chrome=59.0.3071.115
驱动程序信息:chromedriver=2.29.461591 62ebf098771772160f391d75e589dc567915b233,平台=Windows NT
10.0.10240 x86_64警告:服务器未提供任何堆栈跟踪信息
更换以下行 batchVal.get1.click 与 drivers.findElementsBy.classNameresults.geti.click 要避免StaleElementReferenceException
这里发生的事情是,您创建了WebElements对象,然后单击该页面上的。单击后页面的某些部分会发生更改,因此存储的元素会过时,这就是Webdriver告诉您的 只需下载selenium的新chrome扩展文件并使用selenium 3即可。当我钻研后将上一页的数据与新页上的数据进行比较时,就会出现问题:ifall\u elements\u text.get1.equalTransNotrimmed{System.out.printltest通过-在线索赔-日志-钻取结果-交易编号与钻取结果中显示的编号匹配,即+TransNoTrimmed;}这正是我要告诉您的替换代码的地方。如果您将元素存储在列表中,然后使用它,即使页面上有一点小的更改,它也会抛出StaleElementReferenceException。