【问题标题】:retrive value of read only field using Selenium Webdriver使用 Selenium Webdriver 检索只读字段的值
【发布时间】:2016-02-24 17:48:04
【问题描述】:

更新 -- 我在下面添加了整个 HTML 代码,以便于理解问题。我试过使用 xpath findElement(By.xpath(".//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput']")).getText();

我仍然收到错误:

The given selector .//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression .//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput'] because of the following error:
SyntaxError: The expression is not a legal expression.

使用 Selenium Webdriver 我需要检索只读字段的值。 此字段包含我需要与另一个日期进行比较的日期。 我已经尝试了所有方法(通过 id/类名/xpath 等查找元素)它总是返回element not found。我怀疑这是因为输入文件位于一个跨度内,该跨度位于一个 div 内,但对 java 来说是新手,我无法进入该字段。

我正在寻找的价值是<input id="StartDate_txtInput">

HTML 代码如下。

#document
    <!--

    [if lte IE 8]>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD …

    -->
    <!--

    [if gte IE 9]>
    <!DOCTYPE HTML>
    <![endif]

    -->
    <!--

    [if !IE]> 

    -->
    <!DOCTYPE html>
    <!--

     <![endif]

    -->
    <html webdriver="true">

<head id="RuntimeForm"></head>
<body id="RuntimeFormBody" onload="InitialiseRuntimeForm();setInitialSize();" onbeforeunload="return PromptWhenClosing(event)" style="overflow: hidden; margin: 0px;" onresize="resizeContainerCell()" tabindex="-1">
    <form id="Form1" style="height: 100%;" onkeydown="return FormKeyDown(event)" onkeypress="return FormKeyPress(event)" onsubmit="javascript:return WebForm_OnSubmit();" action="RunTimeForm.aspx?ScreenID=8142758e-269a-41f0-b551-433e56dd1225" method="post" name="Form1">

<div></div>
<script type="text/javascript"></script>
<script type="text/javascript" src="/V14_ST1/WebResource.axd?d=5sNVsMkS2BZs-uoFEc4iQlsiyiLjXyZMN…nDsYTHfbaZu1hg-eY3e5am9_qL4BG_2h5iaVRw1&t=635662391674952392"></script>
<script></script>
<script></script>
<script></script>
<script type="text/javascript" src="/V14_ST1/WebResource.axd?d=s_BonnG3MwQl_dl8IfLCFCykZO1rmMJx-…odgFonyPZXsgvo4Ui0T7XvpKyRoUhQlSwxeG541&t=635662391674952392"></script>
<script src="/V14_ST1/UltimateSpellInclude/UltimateSpell.core.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript"></script>
<script type="text/javascript"></script>
<input id="hdAccessKeyArea" type="hidden" value="8" name="hdAccessKeyArea"></input>
<a id="AccessKeyLink" accesskey="0" onactivate="ShowAccessKeyHelp('false',$('#hdAccessKeyArea')[0].value);this.blur();" title="Access Key Help" tabindex="-1" href="javascript:ShowAccessKeyHelp(false,$('#hdAccessKeyArea')[0].value);"></a>
<div id="ValidationSummary" style="color:Red;height:22px;width:197px;z-index:102;left:13px;position:absolute;top:420px;display:none;"></div>
<div id="tblContainer" style="display: block; width: 100%;">

                            <!--

                             Header ToolBar 

                            -->
                            <div id="trToolbarHeaderContainer" style="height: auto; width: 100%;"></div>
                            <!--

                             Runtime form content 

                            -->
                            <div id="Content" style="position:relative; background-color: #F9FAF5; width: 100%; height: 100%;">
                                <!--

                                 Runtime form cotrols 

                                -->
                                <div id="ContainerCell" style="overflow: auto; width: 459px; height: 305px; position: relative; background-color: rgb(250, 250, 250); cursor: default;"></div>
                                <!--

                                 Refresh panel 

                                -->
                                <div id="RefreshPanel" style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; display: none"></div>
                                <!--

                                 Footer ToolBar 

                                -->
                                <div id="trToolbarFooterContainer" style="width: 100%;"></div>
                                <div id="trToolbarMobileContainer" style="width: 100%;"></div>
                                <div id="dialog" title=""></div>
                                ::after
                            </div>
                            <input id="hdSubRecordDelete" type="hidden" name="hdSubRecordDelete"></input>
                            <input id="hdRecordID" type="hidden" value="00000000-0000-0000-0000-000000000000" name="hdRecordID"></input>
                            <input id="hdTableName" type="hidden" name="hdTableName"></input>
                            <input id="hdNoDisplayScreen" type="hidden" value=" No display screen assigned for the current Grid." name="hdNoDisplayScreen"></input>
                            <input id="hdParentRecordID" type="hidden" value="00000000-0000-0000-0000-000000000000" name="hdParentRecordID"></input>
                            <input id="hdHelpURL" type="hidden" name="hdHelpURL"></input>
                            <input id="hdHelpExists" type="hidden" value="html" name="hdHelpExists"></input>
                            <input id="hdSaveRecordFirstMessage" type="hidden" value="You must save the current record before carrying out this action." name="hdSaveRecordFirstMessage"></input>
                            <input id="hdDBSearchAddInID" type="hidden" name="hdDBSearchAddInID"></input>
                            <input id="ReadOnly" type="hidden" value="1" name="ReadOnly"></input>
                            <input id="hdHelpTitle" type="hidden" value=" Help " name="hdHelpTitle"></input>
                            <input id="hdDateLastModified" type="hidden" name="hdDateLastModified"></input>
                            <input id="ChangesAction" type="hidden" value="0" name="ChangesAction"></input>
                            <input id="hdFormLastModified" type="hidden" value="07/01/2015 08:20:11" name="hdFormLastModified"></input>
                            <input id="hdIsChangesMade" type="hidden" name="hdIsChangesMade"></input>
                            <input id="ImpersonationToken" type="hidden" name="ImpersonationToken"></input>
                            <input id="HRImpersonationToken" type="hidden" value="EcwWpmHtjrWmn9UTEYVeW0QB4wsmk2a+IdSkkHq9ENs=" name="HRImpersonationToken"></input>
                            <input id="ImpersonationId" type="hidden" name="ImpersonationId"></input>
                            <input id="InstanceID" type="hidden" value="0538a35f-03d7-e511-968c-8e001800001f" name="InstanceID"></input>
                            <input id="hdScreenId" type="hidden" value="8142758e-269a-41f0-b551-433e56dd1225" name="hdScreenId"></input>
                            <input id="hdScreenIdentifiers" type="hidden" name="hdScreenIdentifiers"></input>
                            <input id="hdUniqueID" type="hidden" value="d0b819a3-47b6-4cf1-a08b-983e98d0a673" name="hdUniqueID"></input>
                            <input id="hdFormDataset" type="hidden" value="AAEAAAD/////AQAAAAAAAAAMAgAAAE5TeXN0ZW0uRGF0YSwgVmVyc2lvbj00…cgZfQnVpbGQJX1JldmlzaW9uAAAAAAgICAgCAAAAAAAAAP//////////Cw==" name="hdFormDataset"></input>
                            <input id="hdRecordIDList" type="hidden" name="hdRecordIDList"></input>
                            <input id="hdVersion" type="hidden" name="hdVersion"></input>
                            <input id="hdAutoCompleteMobileOnly" type="hidden" value="True" name="hdAutoCompleteMobileOnly"></input>
                            <input id="hdWorkflowSubmitted" type="hidden" value="false" name="hdWorkflowSubmitted"></input>
                            <input id="hEventSource" type="hidden" name="hEventSource"></input>
                            <script type="text/javascript"></script>
                            <script type="text/javascript"></script>
                            <script></script>
                            <script type="text/javascript"></script>
                            <script language="javascript" type="text/javascript"></script>
                            <form target="DOCCONTAINER" action="" name="DocName" method="post"></form>
                            <iframe name="DOCCONTAINER" src="javascript:void(0)" style="visibility: hidden; display: none"></iframe>
                            <script type="text/javascript"></script>
                            <script type="text/javascript" language="javascript"></script>
                            <script></script>
                        </div>
                    </form>
                </body>
            </html>
        </html>
    </iframe>
    <script defer="" src="FormContainer.js" type="text/javascript"></script>
    <script type="text/javascript"></script>
</body>

【问题讨论】:

  • 我遇到的一个常见错误是没有切换帧。能否提供更多的 HTML,以便我们查看是否需要切换帧?

标签: java selenium selenium-webdriver


【解决方案1】:

因此,在跨度内不应成为未找到元素的原因。现在请尝试以下两件事:

使用等待:通常加载元素需要一些时间,并且大部分元素未找到问题已通过此修复。

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("StartDate_txtInput")));

使用 getAttribute():您可能正在使用 element.getText() 来检索文本。有时元素中没有文本,而只是将其设置为属性,例如 value 用于 input 标记在这种情况下。试试:

driver.findElement(By.id("StartDate_txtInput")).getAttribute("value");

【讨论】:

    【解决方案2】:

    通过 ID

    driver.findElement(By.id("StartDate_txtInput"));
    

    或使用 CSS 选择器

    input[id="StartDate_txtInput"]
    

    或xpath

    //input[@id='StartDate_txtInput']
    

    或相对xpath

    //div[@id='ContainerSizeGuide']/span/input[@id='StartDate_txtInput']
    

    //div[@id='ContainerSizeGuide']/span/input
    

    在使用这些路径检查 iframe 之前,如果 iframe 中的这个 div 首先你必须转到 iframe 然后使用上面的路径。

    driver.switchTo().frame((WebElement) By.id("iframeID"));
    

    否则你已经使用 webdriver 显式等待

    WebDriverWait wait=new WebDriverWait(driver, 5);
     wait.until(ExpectedConditions.visibilityOfElementLocated(null));
    

    【讨论】:

      【解决方案3】:

      你必须使用属性值而不是文本。

      代码如下:

      String date=driver.findElement(By.id("StartDate_txtInput")).getAttribute("value");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-27
        • 1970-01-01
        • 1970-01-01
        • 2014-05-07
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多