我如何确保所有字段都加载到硒中的页面上,以便 none 过时?

How do I ensure that all fields are loaded on the page in selenium so that none are stale?

问题

我有一个 PeopleSoft 页面,我正在尝试使用 Java 的 Selenium Web 驱动程序对其进行测试。但是,我将 运行 保留为此错误:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

我已阅读 here, here, and here 以及其他 Stack Overflow 问题,none 的答案对我有用。

页面布局

我正在测试的 PeopleSoft 页面是一个 I9 表单,我关心的部分如下所示:

对应的 HTML 确实又大又乱——就像所有 PeopleSoft 页面一样——并且它在它的中间有一个 iframe 几乎包裹了 整个页。

供参考,此 JSFiddle 包含 iframe 中的所有 HTML。

这是与上图中的部分相对应的部分:

<table role='presentation' border='0' id='ACE_width' cellpadding='0' cellspacing='0' class='PSPAGECONTAINER' cols='46' width='963'>
  <!-- OMITTED -->

  <tr>
    <td height='16'></td>
    <td colspan='19' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_LAST_NAMElbl'>
        <label for='G_FORM_I9_G_LAST_NAME' id='G_FORM_I9_G_LAST_NAME_LBL' class='PSEDITBOXLABEL'>*Last Name (Family Name)</label>
      </DIV>
    </td>
    <td colspan='9' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_FIRST_NAMElbl'>
        <label for='G_FORM_I9_G_FIRST_NAME' id='G_FORM_I9_G_FIRST_NAME_LBL' class='PSEDITBOXLABEL'>*First Name (Given Name)</label>
      </DIV>
    </td>
    <td colspan='3' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_MIDDLE_INITIALlbl'>
        <label for='G_FORM_I9_G_MIDDLE_INITIAL' id='G_FORM_I9_G_MIDDLE_INITIAL_LBL' class='PSEDITBOXLABEL'>Middle Initial</label>
      </DIV>
    </td>
    <td colspan='14' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_MAIDEN_NAMElbl'>
        <label for='G_FORM_I9_G_MAIDEN_NAME' id='G_FORM_I9_G_MAIDEN_NAME_LBL' class='PSEDITBOXLABEL'>Other Last Names Used (if any)</label>
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='32' colspan='2'></td>
    <td colspan='19' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_LAST_NAME'>
        <input type='text' name='G_FORM_I9_G_LAST_NAME' id='G_FORM_I9_G_LAST_NAME' tabindex='43' value="Tiger" class='PSEDITBOX' style='width:232px; ' maxlength='30' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
      </DIV>
    </td>
    <td colspan='9' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_FIRST_NAME'>
        <input type='text' name='G_FORM_I9_G_FIRST_NAME' id='G_FORM_I9_G_FIRST_NAME' tabindex='44' value="Tony" class='PSEDITBOX' style='width:160px; ' maxlength='30' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
      </DIV>
    </td>
    <td colspan='2' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_MIDDLE_INITIAL'>
        <input type='text' name='G_FORM_I9_G_MIDDLE_INITIAL' id='G_FORM_I9_G_MIDDLE_INITIAL' tabindex='45' value="" class='PSEDITBOX' style='width:19px; ' maxlength='1' onchange="addchg_win0(this);return doEdits_win0(this,'','N','N','N','N','Y','N',0);"
        />
      </DIV>
    </td>
    <td colspan='14' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_MAIDEN_NAME'>
        <input type='text' name='G_FORM_I9_G_MAIDEN_NAME' id='G_FORM_I9_G_MAIDEN_NAME' tabindex='46' value="" class='PSEDITBOX' style='width:180px; ' maxlength='50' />
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='16'></td>
    <td colspan='21' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_ADDRESSlbl'>
        <label for='G_FORM_I9_G_ADDRESS' id='G_FORM_I9_G_ADDRESS_LBL' class='PSEDITBOXLABEL'>*Address (Street Number and Name)</label>
      </DIV>
    </td>
    <td colspan='5' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_APARTMENT_NBRlbl'>
        <label for='G_FORM_I9_G_APARTMENT_NBR' id='G_FORM_I9_G_APARTMENT_NBR_LBL' class='PSEDITBOXLABEL'>Apt. Number</label>
      </DIV>
    </td>
    <td colspan='7' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_CITYlbl'>
        <label for='G_FORM_I9_G_CITY' id='G_FORM_I9_G_CITY_LBL' class='PSEDITBOXLABEL'>*City or Town</label>
      </DIV>
    </td>
    <td colspan='6' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_STATElbl'>
        <label for='G_FORM_I9_G_STATE' id='G_FORM_I9_G_STATE_LBL' class='PSEDITBOXLABEL'>*State</label>
      </DIV>
    </td>
    <td colspan='6' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_POSTALlbl'>
        <label for='G_FORM_I9_G_POSTAL' id='G_FORM_I9_G_POSTAL_LBL' class='PSEDITBOXLABEL'>*Zip Code</label>
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='32' colspan='2'></td>
    <td colspan='20' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_ADDRESS'>
        <input type='text' name='G_FORM_I9_G_ADDRESS' id='G_FORM_I9_G_ADDRESS' tabindex='47' value="" class='PSEDITBOX' style='width:324px; ' maxlength='50' aria-required='true' />
      </DIV>
    </td>
    <td colspan='6' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_APARTMENT_NBR'>
        <input type='text' name='G_FORM_I9_G_APARTMENT_NBR' id='G_FORM_I9_G_APARTMENT_NBR' tabindex='48' value="" class='PSEDITBOX' style='width:81px; ' maxlength='10' />
      </DIV>
    </td>
    <td colspan='8' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_CITY'>
        <input type='text' name='G_FORM_I9_G_CITY' id='G_FORM_I9_G_CITY' tabindex='49' value="" class='PSEDITBOX' style='width:120px; ' maxlength='30' aria-required='true' />
      </DIV>
    </td>
    <td colspan='5' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_STATE'>
        <input type='text' name='G_FORM_I9_G_STATE' id='G_FORM_I9_G_STATE' tabindex='50' value="" class='PSEDITBOX' style='width:53px; ' maxlength='6' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
        <a class='PSHYPERLINK' name='G_FORM_I9_G_STATE$prompt' id='G_FORM_I9_G_STATE$prompt' tabindex='51' href="javascript:pAction_win0(document.win0,'G_FORM_I9_G_STATE$prompt');">
          <img src="/cs/ps/cache2/PT_PROMPT_LOOKUP_1.gif" alt='Look up State (Alt+5)' title='Look up State (Alt+5)' border='0' align='absmiddle' />
        </a>
      </DIV>
    </td>
    <td colspan='5' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_POSTAL'>
        <input type='text' name='G_FORM_I9_G_POSTAL' id='G_FORM_I9_G_POSTAL' tabindex='52' value="" class='PSEDITBOX' style='width:80px; ' maxlength='12' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='16'></td>
    <td colspan='9' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_BIRTHDATElbl'>
        <label for='G_FORM_I9_G_BIRTHDATE' id='G_FORM_I9_G_BIRTHDATE_LBL' class='PSEDITBOXLABEL'>*Date of Birth (mm/dd/yyyy)</label>
      </DIV>
    </td>
    <td colspan='13' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_SSNlbl'>
        <label for='G_FORM_I9_G_SSN' id='G_FORM_I9_G_SSN_LBL' class='PSEDITBOXLABEL'>*U.S. Social Security Number</label>
      </DIV>
    </td>
    <td colspan='14' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_EMAILIDlbl'>
        <label for='G_FORM_I9_G_EMAILID' id='G_FORM_I9_G_EMAILID_LBL' class='PSEDITBOXLABEL'>Employee&#039;s E-mail Address</label>
      </DIV>
    </td>
    <td colspan='9' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_PHONElbl'>
        <label for='G_FORM_I9_G_PHONE' id='G_FORM_I9_G_PHONE_LBL' class='PSEDITBOXLABEL'>Employee&#039;s Telephone Number</label>
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='9' colspan='2'></td>
    <td colspan='9' rowspan='2' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_BIRTHDATE'>
        <input type='text' name='G_FORM_I9_G_BIRTHDATE' id='G_FORM_I9_G_BIRTHDATE' tabindex='53' value="05/05/1955" class='PSEDITBOX' style='width:71px; ' maxlength='10' onchange="addchg_win0(this);return doEdits_win0(this,'DMDY/450','N','N','N','N','N','N',0);"
        onkeyup="if (isPromptKey(event))DatePrompt_win0('G_FORM_I9_G_BIRTHDATE','G_FORM_I9_G_BIRTHDATE','450',false);return false;" aria-required='true' />
        <a class='PSHYPERLINK' name='G_FORM_I9_G_BIRTHDATE$prompt' id='G_FORM_I9_G_BIRTHDATE$prompt' tabindex='54' onfocus='doFocus_win0(this,true,false);' href="javascript:DatePrompt_win0('G_FORM_I9_G_BIRTHDATE','G_FORM_I9_G_BIRTHDATE$prompt','450',false);">
          <img src="/cs/ps/cache2/PT_CALENDAR_1.gif" alt='Choose a date (Alt+5)' title='Choose a date (Alt+5)' border='0' align='absmiddle' />
        </a>
      </DIV>
    </td>
    <td colspan='6' rowspan='2' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_SSN'>
        <input type='password' name='G_FORM_I9_G_SSN' id='G_FORM_I9_G_SSN' tabindex='55' value="888881125" class='PSEDITBOX' style='width:74px; ' maxlength='9' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
      </DIV>
    </td>
    <td colspan='6'></td>
    <td colspan='16' rowspan='2' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_EMAILID'>
        <input type='text' name='G_FORM_I9_G_EMAILID' id='G_FORM_I9_G_EMAILID' tabindex='58' value="" class='PSEDITBOX' style='width:232px; ' maxlength='60' />
      </DIV>
    </td>
    <td colspan='7' rowspan='2' nowrap='nowrap' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_G_PHONE'>
        <input type='text' name='G_FORM_I9_G_PHONE' id='G_FORM_I9_G_PHONE' tabindex='59' value="" class='PSEDITBOX' style='width:179px; ' maxlength='24' onchange="addchg_win0(this);oChange_win0=this;" />
      </DIV>
    </td>
  </tr>
  <tr>
    <td height='23' colspan='2'></td>
    <td colspan='6' valign='top' align='left'>
      <DIV id='win0divG_FORM_I9_DRV_G_SSN'><span id='G_FORM_I9_DRV_G_SSN$span' onMouseOver="MOpopupObj_win0.StartPopup('win0divG_FORM_I9_DRV_G_SSN', 'G_FORM_I9_DRV_G_SSN_G_POPUP_SSN', 406, 90);" onMouseOut="MOpopupObj_win0.StopPopup(); " class='PSHYPERLINK' style='border-bottom:1px dashed black;padding-bottom:1px;display:inline-block;'><a name='G_FORM_I9_DRV_G_SSN' id='G_FORM_I9_DRV_G_SSN'  ptlinktgt='pt_peoplecode' tabindex='56' onclick='javascript:cancelBubble(event);' href="javascript:submitAction_win0(document.win0,'G_FORM_I9_DRV_G_SSN');"  class='PSHYPERLINK' >View</a></span>
      </DIV>
      <div id="G_FORM_I9_DRV_G_SSN_G_POPUP_SSN" style="visibility:hidden; position:absolute; top:0; left:0;">
        <DIV class='ps_pspagecontainer' id='win0divPSPAGECONTAINER_G_POPUP_SSN'>
          <table role='presentation' border='0' id='ACE_width' cellpadding='0' cellspacing='0' class='PSPAGECONTAINER' cols='2' width='379'>
            <tr>
              <td width='16' height='8'></td>
              <td width='363'></td>
            </tr>
            <tr>
              <td height='56'></td>
              <td valign='top' align='left'>
                <DIV id='win0divG_FORM_I9_'>
                  <table role='presentation' border='0' id='ACE_G_FORM_I9_' cellpadding='0' cellspacing='0' cols='3' width='363' class='PAGROUPBOXLABELINVISIBLE' style='border-style:none'>
                    <tr>
                      <td width='36' height='10'></td>
                      <td width='121'></td>
                      <td width='206'></td>
                    </tr>
                    <tr>
                      <td height='1'></td>
                      <td rowspan='2' valign='top' align='left'>
                        <DIV id='win0divG_FORM_I9_G_SSN$lbl'><span class='PSEDITBOXLABEL'>Social Security #</span> 
                        </DIV>
                      </td>
                    </tr>
                    <tr>
                      <td height='27'></td>
                      <td valign='top' align='left'>
                        <DIV id='win0divG_FORM_I9_G_SSN$'><span class='PSEDITBOX_DISPONLY' id='G_FORM_I9_G_SSN$'>888881125</span>
                        </DIV>
                      </td>
                    </tr>
                  </table>
                </DIV>
              </td>
            </tr>
            <tr>
              <td height='8' colspan='2'></td>
            </tr>
          </table>
        </DIV>
      </DIV>
    </td>
  </tr>

  <!-- OMITTED -->

</table>

运行 Selenium 代码

现在,我正在尝试将数据输入到此表单中,但我一直收到上述异常。当我来到这个页面时,我首先通过一个 SSO 页面来验证我的身份。然后它将我重定向到表单。那里没有问题。这是我执行此操作的代码(请注意,data 是一个包含数据的 POJO,而 driver 是一个 ChromeWebDriver)。

driver.switchTo().frame("TargetContent");

driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).clear();
driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).sendKeys(data.lastName, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_FIRST_NAME")).clear();
driver.findElement(By.id("G_FORM_I9_G_FIRST_NAME")).sendKeys(data.firstName, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_ADDRESS")).clear();
driver.findElement(By.id("G_FORM_I9_G_ADDRESS")).sendKeys(data.address, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_CITY")).clear();
driver.findElement(By.id("G_FORM_I9_G_CITY")).sendKeys(data.city, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_STATE")).clear();
driver.findElement(By.id("G_FORM_I9_G_STATE")).sendKeys(data.state, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_POSTAL")).clear();
driver.findElement(By.id("G_FORM_I9_G_POSTAL")).sendKeys(data.postal, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_EMAILID")).clear();
driver.findElement(By.id("G_FORM_I9_G_EMAILID")).sendKeys(data.email, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_CITIZEN_CHK")).click();
driver.findElement(By.id("G_FORM_I9_DRV_G_EE_SIGNATURE_PB")).click();

我在整个过程中的各个点都遇到了错误。有时它会在尝试将文本放入的第一个字段失败,有时会在第二个字段失败,有时会在后面的字段失败。

尝试的解决方案

为了解决这个问题,我试过调用这个方法:

private void waitForLoad(WebDriver driver)
{
    new WebDriverWait(driver, 30)
            .until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
                    .executeScript("return document.readyState").equals("complete"));
}

我也试过这个:

new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[starts-with(@id, \"G_FORM_I9_\")]")));

我错过了什么?我还需要检查什么以确保这些字段实际上是可访问的?我曾多次尝试在 IntelliJ 中对此进行调试,但我怀疑在断点处停止的 act 会加载页面,所以我从未看到错误,因为没有元素是陈旧的。

看起来有时调用 clear() 会更改您的元素引用,并且在页面更新时,您正在尝试再次定位此元素并找到旧引用,这会导致 StaleElementReferenceExceptionsendKeys().

如果您已经知道遇到问题的字段,可以尝试以下操作:

WebDriverWait wait = new WebDriverWait (driver, 30);
WebElement element = driver.findElement(By.id("G_FORM_I9_G_LAST_NAME"));
element.clear();
wait.until(ExpectedConditions.stalenessOf(element));
driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).sendKeys(data.lastName, Keys.TAB);

但是如果您不知道是哪些字段导致了问题,并且该元素没有过时,您将得到 TimeoutException.