Selenium Java WebDriver无法在页面上找到任何元素。也无法在页面上滚动

时间:2016-06-02 21:47:45

标签: javascript java jquery html selenium

我正在使用selenium java编写自动化测试脚本。 我在测试的其中一个页面遇到了障碍。

给我带来麻烦的页面有以下html代码,我正在尝试访问最后一行的按钮(对不起,长篇帖子 - 想要显示所有div元素,直到我需要访问的按钮):

<document>
        <html>
        <!-- Header -->
        <head>
        <body class="assembly">
        <!-- start /common/cgi_page_context.jsp-->
        <script type="text/javascript" language="javascript">                                  
    if("/shop/cfa/o/maintenance-products-listing-assembly/assembly21247624/WirelessAddonStep" == "/shop/cfa/cgi-page-context"){                 CGIPageContext.relativepath = "";             }                                      $j(document).ready(function() {                 $j(document).trigger("cgi_page_context_update");             });                          </script>
            <!-- end /common/cgi_page_context.jsp-->
            <!-- begin AREA-2 section EN -->
            <!-- START rfx Library Files -->
            <link href="/storestatic/rfx/rfx-SSP.css" type="text/css" rel="stylesheet"/>
            <script src="/storestatic/rfx/rfx.js" type="text/javascript"/>
            <!-- END rfx Library Files -->
            <!-- end of AREA-2 EN -->
            <!-- start /buy/checkout/shopping_cart/cart_setup.jsp-->
            <form id="currentAssembly" method="post" formid="currentAssembly">
            <a id="fromChangeHeadAnchor" href="" value="false">
            <div id="cartResetHupPop" class="genOverlay">
            <div id="changeCartAHUPPopup" class="genOverlay">
            <!-- end /buy/checkout/shopping_cart/popup/reset_hup.jsp-->
            <!-- start /buy/checkout/shopping_cart/popup/business_plans_warning_popup.jsp-->
            <div id="businessPlansPopupDisplay" class="genOverlay">
            <!-- end /buy/checkout/shopping_cart/popup/business_plans_warning_popup.jsp-->
            <!-- start /buy/checkout/shopping_cart/popup/save_load_cart.jsp-->
            <form id="mustChooseOrder" method="post" formid="mustChooseOrder">
            <form id="saveOrder" method="post" action="/shop/cfa/o/maintenance-products-listing-assembly/assembly21247624/WirelessAddonStep?_DARGS=/shop/buy/checkout/shopping_cart/popup/save_load_cart.jsp.saveOrder">
            <form id="saveOrderOnClose" method="post" action="/shop/cfa/welcome?_DARGS=/shop/buy/checkout/shopping_cart/popup/save_load_cart.jsp.saveOrderOnClose">
            <form id="loadOrder" method="post" action="/shop/cfa/o/maintenance-products-listing-assembly/assembly21247624/WirelessAddonStep?_DARGS=/shop/buy/checkout/shopping_cart/popup/save_load_cart.jsp.loadOrder">
            <div id="chooseOrderOverlay" class="genOverlay">
            <!-- end /buy/checkout/shopping_cart/popup/save_load_cart.jsp-->
            <!-- start /buy/checkout/shopping_cart/popup/unavailable_products.jsp-->
            <!-- end /buy/checkout/shopping_cart/popup/unavailable_products.jsp-->
            <div id="miniCartOverlay" class="genOverlayCart"/>
            <button id="show-addToCartErrorPop" class="hidden viewOverlayMid" type="button"/>
            <div id="addToCartErrorPop" class="genOverlayOpt"/>
            <!-- end /buy/checkout/shopping_cart/popup/global_cart_overlays.jsp-->
            <script type="text/javascript">         var SHOPPING_CART_URL = "/shop/cfa/o/shopping-cart?assemblyId=assembly21247624&stepId=WirelessAddonStep";         var HUP_SHOPPING_CART_OVERLAY_FRAGMENT = "/shop/cfa/o/checkout/hup-shopping-cart?decorator=ajax&confirm=true";         var CART_ERROR_OVERLAY_FRAGMENT = "/shop/cfa/o/checkout/error-overlay?decorator=ajax&confirm=true";         var CART_WARNING_OVERLAY_FRAGMENT = "/shop/cfa/o/checkout/warning-overlay?decorator=ajax&confirm=true";         var MINI_CART_OVERLAY_FRAGMENT = "/shop/cfa/o/mini-cart?decorator=ajax&confirm=true";         var MINI_CART_PRODUCT_DETAILS_FRAGMENT = "/shop/cfa/o/checkout/product-details-minicart?decorator=ajax&confirm=true";     </script>
            <!-- end /buy/checkout/shopping_cart/cart_setup.jsp-->
            <!-- start /buy/checkout/error/validate_order_conflict_error.jsp-->
            <!-- end /buy/checkout/error/validate_order_conflict_error.jsp-->
            <!-- start /cfa/overlays.jsp-->
            <!-- end /cfa/overlays.jsp-->
            <!-- start /user/logout/auto_logout_form.jsp-->
            <form method="POST" action="/shop/cfa/home?_DARGS=/shop/user/logout/auto_logout_form.jsp.autoLogoutForm" name="autoLogoutForm">
            <!-- end /user/logout/auto_logout_form.jsp-->
            <script language="javascript" charset="utf-8" src="/storestatic/js/check_browser_close.js?_=20160526-1150" type="text/javascript"/>
            <div id="wrap">
            <div id="wrap_inner">
            <!-- start /browse/navigation_bar/navigation.jsp-->
            <!-- FOR BREADCRUMB SEARCH FORM START -->
            <!-- FOR BREADCRUMB SEARCH FORM END -->
            <!-- start /browse/navigation_bar/top_navigation.jsp-->
            <div id="top-nav" class="emptyCartWarning">
            <!-- start /tools/callcare/call_care_credentials.jsp-->
            <script language="javascript" charset="utf-8" src="/storestatic/js/call_care_service_dev.js?_=20160526-1150" type="text/javascript"/>
            <!-- start /tools/callcare/call_care_unavailable.jsp-->
            <!-- end /tools/callcare/call_care_unavailable.jsp-->
            <!-- end /tools/callcare/call_care_credentials.jsp-->
            <!-- end /browse/navigation_bar/top_navigation.jsp-->
            <!-- start /browse/navigation_bar/main_navigation.jsp-->
            <div id="gen-nav" class="limitedNavigation no-subnav">
            <!-- end /browse/navigation_bar/main_navigation.jsp-->
            <!-- BreadCrumb START -->
            <!-- end /browse/navigation_bar/navigation.jsp-->
            <div id="content-holder">
            <div id="content" class="opt sharedMaintSum">
            <div class="main-wrap">
            <!-- S38580 - Usability Enhancement - Collapsible Devices Section - Changes End -->
            <!-- Begin Assembly Steps -->
            <!-- start /customer/dashboard/maintenance/maintenance.jsp-->
            <input id="dpFlag" type="hidden" value="false"/>
            <div class="marginTop15">
            <div class="browseHeader">
            <!-- start /customer/dashboard/maintenance/steps/assembly_steps.jsp-->
            <!-- start /customer/dashboard/maintenance/steps/change_plan_addons_addons_header.jsp-->
            <!-- start /customer/dashboard/maintenance/steps/wireless_header.jsp-->
            <div class="assemblyBoxName ">
            <!-- end /customer/dashboard/maintenance/steps/wireless_header.jsp-->
            <!-- end /customer/dashboard/maintenance/steps/change_plan_addons_addons_header.jsp-->
            <div class="assemblySteps noMargin current" style="display: block;">
            <!-- end /customer/dashboard/maintenance/steps/assembly_steps.jsp-->
            <!-- start /browse/assembly/totalshared_fragment.jsp-->
            <!-- end /browse/assembly/totalshared_fragment.jsp-->
            <!-- start /customer/dashboard/maintenance/include/maintenance_billing_fragment.jsp-->
            <div class="assemblyStepsCurrentlyBilled">
            <!-- end /customer/dashboard/maintenance/include/maintenance_billing_fragment.jsp-->
            <!-- start /customer/dashboard/maintenance/steps/change_plan_addons_addons_footer.jsp-->
            <!-- start /customer/dashboard/maintenance/steps/wireless_footer.jsp-->
            <input id="subsidyOffersPopupUrl" type="hidden" value="/shop/cfa/o/subsidy_offers_popup?decorator=ajax&confirm=true"/>
            <input id="skuId" type="hidden" value="IP616GLD"/>
            <input id="pricePlanProductCode" type="hidden" value="LTOM22006"/>
            <input id="isSimOnlyActivation" type="hidden" value=""/>
            <input id="newPlanTerm" type="hidden" value="monthly"/>
            <input id="isNetNewActivation" type="hidden" value="false"/>
            <!-- Voice mail deletion start -->
            <!-- boolean used to differentiate the PPC and ATE -->
            <!-- boolean used to differentiate the PPC and ATE -->
            <input id="isVoiceMailRequired" type="hidden" value="false"/>
            <!-- Voice mail deletion end -->
            <!-- R516-UCC FMCAddon Deleted popup -->
            <form id="submitMaintenance" method="post" action="/shop/cfa/o/maintenance-products-listing-assembly/assembly21247624/WirelessAddonStep?_DARGS=/shop/customer/dashboard/maintenance/steps/wireless_footer.jsp.submitMaintenance">
            <form id="validateAssembly_WirelessAddonStep_assembly21247624" class="validateAssemblyForm" method="post" action="/shop/cfa/o/maintenance-products-listing-assembly/assembly21247624/WirelessAddonStep?_DARGS=/shop/customer/dashboard/maintenance/steps/wireless_footer.jsp.validateAssembly_WirelessAddonStep_assembly21247624">
            <div class="assemblyStepsFooter shared ">
            <div class="subTotal blackGradient">
            <div class="assebmlyButton blackGradient">
            <div class="buttonWrap">
            <a id="show-cancelMaintenancePop" class=" viewOverlayMid" href="#">Cancel</a>
            <!-- start /customer/dashboard/maintenance/add_subscriber/add_another_subscriber_button.jsp-->
            <input id="subsidyPopupUrlShared" type="hidden" value="/shop/cfa/o/subsidy_offers_popup?decorator=ajax&confirm=true"/>
            <input id="skuIdShared" type="hidden" value="IP616GLD"/>
            <input id="pricePlanProductCodeShared" type="hidden" value="LTOM22006"/>
            <!-- end /customer/dashboard/maintenance/add_subscriber/add_another_subscriber_button.jsp-->
            <!-- start /browse/assembly/include/add_another_shared_device_button.jsp-->
            <input id="subsidyPopupUrlShared" type="hidden" value="/shop/cfa/o/subsidy_offers_popup?decorator=ajax&confirm=true"/>
            <input id="skuIdShared" type="hidden" value="IP616GLD"/>
            <input id="pricePlanProductCodeShared" type="hidden" value="LTOM22006"/>
            <!-- end /browse/assembly/include/add_another_shared_device_button.jsp-->
            <!-- button introduced as part of R1014 to validate the device protection -->
            <div id="validateDeviceProtectionATE" class="hidden assemblyValidationButtonSection">
            <div id="validatedAssembly" class=" assemblyValidationButtonSection">
            <button id="validateAssembly" class="button cta addToCartGrayOverlay">I'm Done</button>
            <input class="assemblyOfferPopupButtonLabel" type="hidden" value="done"

 Done"/>
        </div>
        </div>
        </div>

我尝试在页面上滚动,等待某些元素(显式和隐式),我甚至尝试访问第一个页面元素,希望选择按钮进入按钮。

我不断收到StaleElementReference异常,这意味着该元素已被破坏并在某个时刻重新创建。

我也尝试过本网站的各种解决方案,但无济于事。

请在下面找到我当前的尝试代码:

String timeStamp;

        try
        {

         timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
          atThisStage=timeStamp +": Going to Summary Page (Clicking I'm Done)";
          System.out.println(atThisStage);

          //driver.switchTo().frame(driver.findElement(By.id("currentAssembly")));
          //Stale Element Reference
          WebDriverWait wait = new WebDriverWait(driver, 60);
          WebElement form = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='bread']/li[2]"))); 
          Thread.sleep(2000);
          form.sendKeys(Keys.TAB);
          form.sendKeys(Keys.TAB);
          form.sendKeys(Keys.TAB);
          form.sendKeys(Keys.TAB);
          form.sendKeys(Keys.TAB);

          //Scroll Down
          ((JavascriptExecutor)driver).executeScript("scroll(0,400)");

          /*
          WebDriverWait wait1 = new WebDriverWait(driver,15);
          WebElement select = wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='content']"))); 
          Actions action = new Actions (driver);
          action.moveToElement(driver.findElement(By.xpath(".//*[@id='content']")));      //.move_to_element(driver.find_element_by_id('offertags')).click();
          action.sendKeys(Keys.PAGE_DOWN).perform();                                      //send_keys(Keys.PAGE_DOWN).perform();
          */

          //
          //JavascriptExecutor jse1 = (JavascriptExecutor)driver;
          //jse1.executeScript("scroll(0, 600)"); //y value can be altered

          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

          //WebElement imDone = driver.findElement(By.xpath(".//*[@id='validateAssembly'] [@class='button cta addToCartGrayOverlay']"));
          //driver.findElement(By.xpath(".//*[@id='validateAssembly'] {"));
          //WebElement imDone = driver.findElement(By.id("validateAssembly"));
          //imDone.click();

          /*
          //Review Order Page 
          JavascriptExecutor jse2 = (JavascriptExecutor)driver;
          jse2.executeScript("scroll(0, 1200)"); //y value can be altered
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

          WebElement reviewOrder = driver.findElement(By.xpath(".//*[@id='printServiceAgreement']"));
          reviewOrder.toString();

          driver.close();

          timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
          atThisStage=timeStamp +": Viewed Order Summary. Proceeding to Sign Out";
          System.out.println(atThisStage);

          WebElement signOut = driver.findElement(By.xpath(".//*[@id='signOutLnk']"));
          signOut.click();
          */
        }

        catch (Exception e)
        {
            timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
              atThisStage=timeStamp +": Unable to Proceed to Order Summary";
              System.out.println(atThisStage);

              File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

              try {
                FileUtils.copyFile(scrFile, new File(screenCapturePath));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

              //Send Notification E-mail
              try {
                JavaMail.sendMail(atThisStage, screenCapturePath, prop);
            } catch (MessagingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

              timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());     
              atThisStage=timeStamp +":[ERROR]" +e.toString();
              System.out.println(atThisStage);

              driver.quit();
        }

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

因此,当您尝试直接找到该元素时,您仍会获得StaleElementReferenceException,或者它根本找不到它?您可以捕获该异常并尝试新的findElement

(很抱歉作为答案回答,还没有足够的声誉发表评论)