Selenium 未导航分页3并获取过时元素异常

Selenium 未导航分页3并获取过时元素异常,selenium,selenium-webdriver,pagination,staleelementreferenceexception,Selenium,Selenium Webdriver,Pagination,Staleelementreferenceexception,我需要向下或向上导航页面并检查每个页面上的服务,但在导航第3页时,错误消息显示为陈旧元素异常 我已经尝试过这个代码,但它的工作不好,有人请帮助我在这方面 public class TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer { public static void TC_2377154_12236() { try { Assert.done("Start -> TC_2377154

我需要向下或向上导航页面并检查每个页面上的服务,但在导航第3页时,错误消息显示为陈旧元素异常

我已经尝试过这个代码,但它的工作不好,有人请帮助我在这方面

public class TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer {
    public static void TC_2377154_12236() {
        try {
            Assert.done("Start -> TC_2377154_12236");

            //BaseSetUp.openBrowser();
            WebDriver driver = FrameworkDriver.driver;
            driver.get("https://webtech.humana.com/nes/servicesview.aspx"); 
            Assert.assertTrue(driver.findElement(By.xpath("//input[@id='tbLBVServerName']")).isDisplayed(), "LBV Server name is displayed");
            driver.findElement(By.xpath("//input[@id='tbLBVServerName']")).sendKeys("SRS");

            Assert.assertTrue(driver.findElement(By.xpath("//input[@id='btnFilter']")).isDisplayed(),"Filter button is displayed");
            driver.findElement(By.xpath("//input[@id='btnFilter']")).click();

            Assert.assertTrue(driver.findElement(By.xpath("//table[@id='dgServices']")).isDisplayed(),"Services name table is Displayed");
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until( ExpectedConditions.presenceOfElementLocated(
                                          By.xpath("//a[@style='color:Black;']")));

            List<WebElement> pagination = driver.findElements(By.xpath("//a[@style='color:Black;']"));
            int s = pagination.size()/2;
            System.out.println(s +"Pagination list");
            for(int i=0;i<=s;i++){
                List<WebElement> ls = driver
                        .findElements(By
                                .xpath("//font[@color='red']"));
                System.out.println(ls.size()+"list of Down services size");
                if (ls.size() > 1)
                        Assert.fail("All service states are expected Up and Running but there are "+(ls.size()+" servers are down")); 
                else if (ls.size() == 0)
                    Assert.fail("****Some technical issue or No Server is up or running****");
                else if (ls.size() == 1)
                    Assert.pass("~~All Servers are Up and running~~");

                    Thread.sleep(5000);

                //  ls.get(i).click();
                 // Thread.sleep(5000);
                 System.out.println( pagination.get(i)); 
                 //driver.findElement(By.xpath("//a[@style='color:Black;']")).click();
                 pagination.get(i).click();

           Thread.sleep(5000);
        }

        Assert.done("End -> TC_2377154_12236");

        }  catch (Exception e) {

            Assert.error(e, "Error -> TC_2377154_12236");
        }
    }
}

不要从For循环中检索多个元素。 您应该再次在For循环中找到每个元素

WebElement pag=diver.findElement(By.xpath(“(//a[@style='color:Black;'])[”+(i+1)+“])”)

Xpath示例(您可以在web控制台中搜索):

(//a[@style='color:Black;'])[1]

List pagination=driver.findElements(By.xpath(//a[@style='color:Black;']);
int s=pagination.size()/2;
System.out.println(s+“分页列表”);
对于(int i=0;i 1)
Assert.fail(“所有服务状态都应处于启动和运行状态,但存在“+(ls.size()+”服务器处于关闭状态”);
else if(ls.size()==0)
Assert.fail(“****某些技术问题或没有服务器启动或运行****”;
else if(ls.size()==1)
Assert.pass(“~~所有服务器都已启动并正在运行~~”;
睡眠(5000);
//ls.get(i.click();
//睡眠(5000);
System.out.println(分页.get(i));
//findElement(By.xpath(“//a[@style='color:Black;']))。单击();
//修改此行
pag.click();
睡眠(5000);
}
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : PASS - Filter button is displayed
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : PASS - Services name table is Displayed
7Pagination list
5list of Down services size
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : FAIL - All service states are expected Up and Running but there are 5 servers are down
[[ChromeDriver: chrome on XP (4f932cb86e8057b5ecda92c582500243)] -> xpath: //a[@style='color:Black;']]
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : FAIL - Error -> TC_2377154_12236.  Error:stale element reference: element is not attached to the page document
  (Session info: chrome=77.0.3865.90)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'WKR90LDWJ0', ip: '10.94.27.177', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_192'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.37.544315 (730aa6a5fdba15..., userDataDir: C:\Users\VAM1449\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 77.0.3865.90, webStorageEnabled: true}
Session ID: 4f932cb86e8057b5ecda92c582500243 Stack Trace: [Ljava.lang.StackTraceElement;@18d3f76
FAIL Error:stale element reference: element is not attached to the page document
  (Session info: chrome=77.0.3865.90)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'WKR90LDWJ0', ip: '10.94.27.177', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_192'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.37.544315 (730aa6a5fdba15..., userDataDir: C:\Users\VAM1449\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 77.0.3865.90, webStorageEnabled: true}
Session ID: 4f932cb86e8057b5ecda92c582500243 Stack Trace: 
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=77.0.3865.90)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'WKR90LDWJ0', ip: '10.94.27.177', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_192'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.37.544315 (730aa6a5fdba15..., userDataDir: C:\Users\VAM1449\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 77.0.3865.90, webStorageEnabled: true}
Session ID: 4f932cb86e8057b5ecda92c582500243
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at com.humana.hss.scripts.TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer.TC_2377154_12236(TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.dell.acoe.framework.run.ExecutionThread.run(ExecutionThread.java:33)
Executing method [TC_2377154_12236]... completed
There is an issue while updating the script status to excel report. Please check file:null
Total:2
Passed:1  50%
Custom Report Generator is configured as: 
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : DONE - Custom Report Generator is configured as: 
 is not a valid CustomReportGenerator, it needs package.Class.method format and with two String args
TC_2377154_12236_Verify_SRS_ServerStatus_LinuxServer-TC_2377154_12236 : DONE -  is not a valid CustomReportGenerator, it needs package.Class.method format and with two String args
        List<WebElement> pagination = driver.findElements(By.xpath("//a[@style='color:Black;']"));

        int s = pagination.size()/2;
        System.out.println(s +"Pagination list");

        for(int i=0;i<=s;i++){
            // Add this line
            WebElement pag = diver.findElement(By.xpath("(//a[@style='color:Black;'])["+(i+1)+"]"));

            List<WebElement> ls = driver.findElements(By.xpath("//font[@color='red']"));
            System.out.println(ls.size()+"list of Down services size");
            if (ls.size() > 1)
                    Assert.fail("All service states are expected Up and Running but there are "+(ls.size()+" servers are down")); 
            else if (ls.size() == 0)
                Assert.fail("****Some technical issue or No Server is up or running****");
            else if (ls.size() == 1)
                Assert.pass("~~All Servers are Up and running~~");

                Thread.sleep(5000);

            //  ls.get(i).click();
             // Thread.sleep(5000);
             System.out.println( pagination.get(i)); 
             //driver.findElement(By.xpath("//a[@style='color:Black;']")).click();

             // Modify this line
             pag.click();

       Thread.sleep(5000);
    }