使用Selenium Webdriver读取只读字段的值

时间:2016-02-24 17:48:05

标签: java selenium selenium-webdriver

更新 - 我已添加以下整个HTML代码,以便更轻松地了解该问题。我尝试过使用xpath 的 findElement(By.xpath( “.// DIV [@ ID = 'ContainerCell'] /跨度/ [@ ID = '开始日期'] /输入[@ ID = 'StartDate_txtInput']”))。gettext的()

我仍然收到错误: 给定的选择器.//div [:id ='CONTinerCell'] / span / [@ id ='StartDate'] / input [@id ='StartDate_txtInput']无效或不会产生WebElement。发生以下错误: InvalidSelectorError:由于以下错误,无法找到带有xpath表达式的元素.//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput'] SyntaxError:表达式不是合法表达式。

使用Selenium Webdriver我需要检索只读字段的值。 此字段包含我需要与另一个日期进行比较的日期。 我已经尝试了所有方法(通过id / class name / xpath等查找元素)它总是返回未找到的元素。我怀疑这是因为输入文件位于一个跨越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>

3 个答案:

答案 0 :(得分:0)

所以在一个范围内不应该是Element Not Found的原因。现在请尝试以下两件事:

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

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

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

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

答案 1 :(得分:0)

通过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,那么请使用上面的nay路径。

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

否则你明确等待使用webdriver

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

答案 2 :(得分:0)

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

代码如下:

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