如何将上一页的值与webdriver中新打开的页面中的值进行比较

时间:2017-06-30 11:13:53

标签: java selenium

我试图将用户点击“交易编号”链接后,页面中显示的交易编号与下一页显示的交易编号进行比较。

enter image description here

我尝试了以下内容:

//  -----------------------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;
        }

当我运行时,我收到以下错误:

  

线程中的异常&#34; main&#34;   org.openqa.selenium.StaleElementReferenceException:陈旧元素   reference:元素未附加到页面文档         (会议信息:chrome = 59.0.3071.115)         (驱动程序信息:chromedriver = 2.29.461591(62ebf098771772160f391d75e589dc567915b233),platform = Windows NT   10.0.10240 x86_64)(警告:服务器未提供任何堆栈跟踪信息)

2 个答案:

答案 0 :(得分:1)

替换以下行。

batchVal.get(1).click();

drivers.findElements(By.className("results")).get(i).click();

避免 StaleElementReferenceException

这里发生的事情是,因为您创建了WebElements的对象,然后您点击该页面。点击后页面的某些部分会发生变化,因此存储的元素会变得陈旧,这就是Webdriver告诉你的内容。

答案 1 :(得分:0)

只需下载selenium的新chrome扩展文件并使用selenium 3.