在用户单击“事务号”链接后,我试图将页面中显示的事务号与下一页中显示的事务号进行比较。
我尝试了以下几点:
// -----------------------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;
}
当我运行这个程序时,我会得到以下错误:
线程"main“org.openqa.selenium.StaleElementReferenceException:陈旧元素引用中的异常:元素未附加到页面文档(会话信息: chrome=59.0.3071.115) (驱动程序信息: chromedriver=2.29.461591 (62ebf09877172160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (警告:服务器没有提供任何堆栈跟踪信息)
发布于 2017-06-30 03:19:23
替换下面一行。
batchVal.get(1).click();
使用
drivers.findElements(By.className("results")).get(i).click();
为了避免StaleElementReferenceException
这里发生的事情是,您已经创建了WebElements对象,然后单击该页面。单击后页面的某些部分会发生变化,因此存储的元素会变得陈旧,这就是Webdriver告诉您的。
发布于 2017-06-30 05:37:17
只需下载selenium的新铬扩展文件并使用selenium 3即可。
https://stackoverflow.com/questions/44845132
复制